/* Monetary policy MOnte Carlo VAR analysis: feeding interest rate shocks only */
/* By Toshihiro OKADA */


//** the time period of the simulated siries: ts=start te=end **
ts=1001; te=1190;
inds=201; inde=390; %**

//***** Choosing a set of parameters (posterior modes): Basic NK, Standard NK, and EPD-NK models *****//  
@#define data_techzero=1 
//* 0: Basic or Standard NK                      *//
//* 1: EPD-NK                                    *// 


//********** Model choice ********* //
@#define tech =1
//* 1: EPD NK                *// 
//* 0: Basic or Standard NK  *//


//**** Shutting down endogenous technology for EPD-NK model ***// 
@#define ashock=1
//* 1: with endogenous tech.     *//
//* 0: without endogenous tech.  *//
//* 2: arbitrary value for "alfa" (R&D elasticity)  *//
alfa_n = 150; /* to shut down technology effect */
mode_adj =1.0; /* Not used: only go with ashock==2 */


//*** Price index **//
@#define pindex=0
//* pindex==0: no price index  *//
//* pindex==1: with price index *//
//*******************************//

//*****************Interest lagged effect *******************//
@# define lag_i=0
//* lag_i=1: interest rate has one lagged efect *//
//* lag_i=0: interest rate has contemporary effect *//
//************************************************************//

//*****************Inflation VAR IRF adjustment *******************//
@# define var_price=1 
//* =0: No adjustment (infaltion)*//
//* =1: adjustmendt (price IRF first�@difference *//
//************************************************************//


//********Degree of shock contribution for Interest VAR simulation**********************//
//* To make the followings work, choose 0 for all types of shocks in"shock specification" section below *//
shk_c_=0.00001;
shk_h_=0.00001;
shk_f_=0.00001;
shk_u_=0.00001;
shk_eue_=0.00001;
shk_tfp_=0.00001;
shk_i_=1;
//** only interest rate shocks are fed **//

//***** Shock Specification for monte carlo **//
@#define ushock=0
@#define news=0
@#define dshock=0
@#define cshock=0
@#define hshock=0
@#define ishock=0
@#define fshock=0
@#define tfpshock=0
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//* ushock: ushock=0 shut down technology(u)shocks, 2: news: news=0, shut down  news shock but leave u shocks, cshock: consumption shock, hshock: hour shocks, ishock:interest rate shock *// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//** croos correaltion of shocks **//
@#define crosscrr=1 

@#define var_variable=1
//** Not used **//

//** Not used **//
@#define monte_bays_irf=0
@#define manu_irf=1

//** Not used **//
@#define irf_bands=2

//*** TTB spec and news shock spec **//
@#define ttb_spec=8
@#define eshock=16

@#define news_period=16
/* news shocks periods */ 

@#define bayesian_irf=0
//** Not used **//

@#define bays_irf=0
//** Not used **//

//** Not used **//
@#define bays_irf_non_news=0

@#define check=0
//** Not used **//

@#define monte_irf=0
//** Not used **//

//** Not used **//
@#define sim_var=1

//** Not used **//
@#define unrealized=1
//** DO NOT TOUCH ***// 

@#define unrealized_news_sim=0 
//** DO NOT TOUCH ***// 
//** Not used **//

//** Not used **//
@#define var_est_method=0


@#define var_shock=0
//** Not used **//

@#define median_mean=1
/** 1=median, 2=mean **/

@#define var_data=22
//** Not used **//

@#define conf_var=2
//** Not used **//


@#define sim_unrealized=0
//** Not used **//


@#define method=1
//** Not used **//

@#define mx_unrealize=1
//** Not used **//
  
wt_=1;
//** Not used **//

//** Not used **//
@#define bps=0
/* Bandpass filtering: bps=1: band pass filtering is "on" */
@#define cmn_trd=0
/* common trend for Bpass */
@#define ctrd_spec=1
/* common trend specification: 1=no invest, 2=with invest */
@#define hp=0
/* HP filtering */

////////////////////////////////////////////////////
////** Line specification for figures **////////////
///////////////////////////////////////////////////
lsp =2; /* setting a line specification for figures: 1='-', 2='--', 3=':' and 4='-.' */


@#define trend_sigma=0
//** Not used **//

@#define tfp_errors=0
//** Not used **//

tfpwt_ =1.0;
alfaj_=1.00; 



//** Not used ** //
@#define growth=0
@#define std_spec =0

//******** Conditional varinace; on-off ****//
@#define cndvar=0
/** cndvar=1: for (theoretical) conditional variance calculation (std=estimated values) **/  
/** cndvar=0: for otherwisee **/
//*******************************************//

//** Not used **//
@#define fig =0
/** fig=1: for posterior mean(/mode) Non bayesian IRFs(std=0.01) & conditional variance **/
/** fig=0: for monte carlo simulations for VARs&CORRs and baysian IRFs **/  
//** NOTE: FOR fig=1 DO NOT PEFORM MONTE CARLO: i.e, choose only one shock and the mode of coefficient dist **/  


@#define fig_series=0
@#define full_fig=0


//***********************************************//
//******** Loading data ************************//
//**********************************************//

@#if data_techzero==1
  
 @#if ttb_spec==8
  @#if eshock==16

//**e16**//
/* larger eu shocks sigma */ 

load('subdraws_phi60stv90mm5_mode.mat', 'aa0');
  @#endif
 @#endif
@#endif

@#if data_techzero==0
  
 @#if ttb_spec==8
  @#if eshock==16

 //**e16**//
 /* larger eu shocks sigma */ 

//* Without Indexization *// 
@#if pindex==0
load('subdraws_BNKphi60stv90m3_mode.mat', 'aa0');
@#endif

//* With Indexization *// 
 @#if pindex==1
load('subdraws_BNKphi60stv90index_mode.mat', 'aa0');
 @#endif

 @#endif
@#endif

@#endif
//***********************************************//

//**** Not used ***//
@#define large_sample=0
sim_period=190;
//********************************//

me_std=0.00; 
mee_std=0.0; 
meee_std=0.0;

//**********************************//
//********SETTING SEEDS***********//
//**********************************//

//**** Not used ***//
dma=1;
dmb=1;

//**** Not used ***//
jrdma=1; /* seed for news shock simulation random numbering*/
jrdmb=1; /* seed for news shock simulation random numbering*/

//**** Random shock seeds for simulations for monte-carlo VAR simulations (for sim_var=1)*****//
iia=1; /* start of random shock seed # */
iib=3000; /* end of random shock seed # */
iia_=iia-iia+1; /* start of random shock seed # */
iib_=iib-iia+1; /* end of random shock seed # */



//**** Not used: Bayesian coefficient estimates random shock seeds ***********************//
ja=1;/* start of sub draws random shock seed # */
jb=1;/* end of sub draws random shock seed # */



@#define fullshock=1


ex_time_=4; //* if e8 shock is chosen, chose ex_time_=8 . For a single unrealized news shock IRF *// 
@#define ex_time_periods=16

@#if fullshock==1
 @#define shki =1
 @#define shku =1
 @#define shkuc =1
 @#define shkuh =1
 @#define shkue =1
 @#define shkf =1

 @#define shkue1 =1
 @#define shkue2 =1
 @#define shkue3 =1
 @#define shkue4 =1
 @#define shkue5 =1
 @#define shkue6 =1
 @#define shkue7 =1
 @#define shkue8 =1
 @#define shkue9 =1 
 @#if eshock==16
   @#define shkue10 =1 
   @#define shkue11 =1 
   @#define shkue12 =1 
   @#define shkue13 =1 
   @#define shkue14 =1 
   @#define shkue15 =1
  @#define shkue =1
 @#define shkuc =1
 @#define shkuh =1
@#define shkf =1
  @#endif  

 @#endif 



 @#define shock_nm=1
// ** not used **//


//* j= bayesyean coefficients*//
for j=ja:jb

@#if tech==1
 @#if var_data==22
  @#if lag_i==0
  var tfpe, y_g, c_g, tiv_g,iv_g,  h_g, w_g, tfp_g, gg, ggn, uf, pai,y,tfp,tfpu,u,a, conf, conf12, conf11, conf10, conf9, conf8,c,k,iv,h,w,r,rr,p,ppai,m,pf,pf_exp,i,lamda,ud,v,pain,yn,kn,ivn,hn,cn,wn,rn,pn,pfn,in,lamdan,yg,hg,fpai,mcgap,rd,rdn,an,nf,nfn,pinf,uc,um,uh,tiv, tivn ynobs,mcgape, mc, mcn  mce, fpaie, paie, paiee,ppaie, inftr,  tfpn, tq,tqn, shu, shi, shc, shh, shue1 shue3 shue4 shue8  aa aan sp stk  ;
  @#endif
  @#if lag_i==1
  var tfpe, lagin lagi y_g, c_g, tiv_g,iv_g,  h_g, w_g, tfp_g, gg, ggn, uf, pai,y,tfp,tfpu,u,a, conf, conf12, conf11, conf10, conf9, conf8,c,k,iv,h,w,r,rr,p,ppai,m,pf,pf_exp,i,lamda,ud,v,pain,yn,kn,ivn,hn,cn,wn,rn,pn,pfn,lamdan,yg,hg,fpai,mcgap,rd,rdn,an,nf,nfn,pinf,uc,um,uh,tiv, tivn ynobs,mcgape, mc, mcn  mce, fpaie, paie, paiee,ppaie, inftr,  tfpn, tq,tqn, shu, shi, shc, shh, shue1 shue3 shue4 shue8  aa aan sp stk  ;
  @#endif
 @#endif


@#if eshock==16
varexo ei eu euc euh ef eue1 eue2 eue3 eue4 eue5 eue6 eue7 eue8 eue9 eue10 eue11 eue12 eue13 eue14 eue15 eue me mee meee etfp ;
@#endif 

@#if ttb_spec==8
parameters shk_tfp, shk_eue, shk_c, shk_h, shk_f, shk_i,shk_u, tau,  hb,  xxx, rhoi,xpai, xy, gamma, delta, phi, theta, rho, psi, epsilon,   alfa, rhoum,rhouc, rhouh, rhou,rhoud,rhouf, sigc, sigh, sigm, ss, rho_eta, eta1, eta2, eta3, eta4,eta5,eta6,eta7,eta8,iss, rss, yss, kss, css,wss,mss, rdss, pfss,  hss, ivss, lamdass, nfss,x,ggss;
@#endif
 
@#endif

@#if tech==0
@#if var_data==22
 @#if lag_i==0
  var tfpe, y_g, c_g, iv_g, h_g, w_g, tfp_g, gg, ggn, uf, pai,y,tfp, tfpu,u,conf, conf12, conf11, conf10, conf9, conf8,c,k,iv,h,w,r,rr,p,ppai,m,i,lamda,ud,v,pain,yn,kn,ivn,hn,cn,wn,rn,pn,in,lamdan,yg,hg,mcgap,uc,um,uh, ynobs, fpai,mcgape, mc,mcn,mce, fpaie, paie, paiee,ppaie,  tfpn,tq,tqn,shu, shi, shc, shh, shue1 sp  stk ;
 @#endif
 @#if lag_i==1
  var tfpe, lagin lagi y_g, c_g, iv_g, h_g, w_g, tfp_g, gg, ggn, uf, pai,y,tfp, tfpu,u,conf, conf12, conf11, conf10, conf9, conf8,c,k,iv,h,w,r,rr,p,ppai,m,i,lamda,ud,v,pain,yn,kn,ivn,hn,cn,wn,rn,pn,lamdan,yg,hg,mcgap,uc,um,uh, ynobs, fpai,mcgape, mc,mcn,mce, fpaie, paie, paiee,ppaie,  tfpn,tq,tqn,shu, shi, shc, shh, shue1 sp  stk ;
 @#endif
@#endif

@#if eshock==16
varexo ei eu euc euh ef eue1 eue2 eue3 eue4 eue5 eue6 eue7 eue8 eue9 eue10 eue11 eue12 eue13 eue14 eue15 eue me mee meee etfp;
@#endif

 @#if pindex==0
 parameters shk_tfp,shk_eue, shk_c, shk_h, shk_f,shk_i, shk_u,tau, hb,  xxx, rhoi,xpai, xy, gamma, delta, phi, theta, rho, rhoum,rhouc,rhouh, rhou,rhoud,rhouf, sigc, sigh, sigm, ss, iss_noa, rss_noa, yss_noa, kss_noa, css_noa,wss_noa,mss_noa,  hss_noa, ivss_noa, lamdass_noa, ggss_noa ;
 @#endif
 @#if pindex==1
 parameters index, shk_tfp,shk_eue, shk_c, shk_h, shk_f,shk_i, shk_u,tau, hb,  xxx, rhoi,xpai, xy, gamma, delta, phi, theta, rho, rhoum,rhouc,rhouh, rhou,rhoud,rhouf, sigc, sigh, sigm, ss, iss_noa, rss_noa, yss_noa, kss_noa, css_noa,wss_noa,mss_noa,  hss_noa, ivss_noa, lamdass_noa, ggss_noa ;
 @#endif
 
@#endif



shk_c=shk_c_;
shk_h=shk_h_;
shk_f=shk_f_;
shk_eue=shk_eue_;
shk_i=shk_i_;
shk_u=shk_u_;
shk_tfp=shk_tfp_;

tfpwt=tfpwt_;
alfaj=alfaj_;

@#if tech==1

//** data_techzero==1 section **//

@#if data_techzero==1

@#if eshock==16
 rho_eta=aa0(j,17);
 xxx = aa0(j,18);
 xpai = aa0(j,19); 
 xy =aa0(j,20); 


 hb=aa0(j,21); % Internal habit formation param.
 rho =aa0(j,22);  //**  rho=0 is flexible price
 sigc =aa0(j,23); 
 sigm = 2.56; 
 ss=aa0(j,24);
 phi = aa0(j,25);
 psi = aa0(j,26);

 rhou = aa0(j,27);
 rhoi = aa0(j,28);
 rhoud =0.5;
 rhouh = aa0(j,29); 
 rhouc = aa0(j,30); 
 rhouf = aa0(j,31); 


 @#if ashock==1
 alfa =alfaj*aa0(j,32);
 @#endif
 @#if ashock==0
 alfa =alfa_n;
 @#endif
@#endif


rhoum =0.0;
//sigc=1.00;
gamma =0.99; % discount factor  
delta = 0.10000000/4;  %capital depreciation rate
theta = 1/3;
notec=0;
epsilon = 0.1/4;
tau =0.18;
//phi=6.0;
sigh = 2.0;

@#if ttb_spec==8
eta1=( (1+rho_eta+(rho_eta^2)+(rho_eta^3)+(rho_eta^4)+(rho_eta^5)+(rho_eta^6)+(rho_eta^7))^(-1) );
eta2=eta1*(rho_eta);
eta3=eta1*(rho_eta)^2; 
eta4=eta1*(rho_eta)^3;
eta5=eta1*(rho_eta)^4;
eta6=eta1*(rho_eta)^5;
eta7=eta1*(rho_eta)^6;
eta8=eta1*(rho_eta)^7;
@#endif

iss = (1/gamma)-1;
rss = iss + delta;
wss = (((phi-1)/phi)*(1-theta)*(((1-theta)/theta)^(-theta))*(rss^(-theta)))^(1/(1-theta));
yss_hss = ((theta/(1-theta))*(wss/rss))^theta;
kss_hss = (yss_hss)^(1/theta);
nfss= (1-psi)/epsilon;


@#if ttb_spec==8
g1_t8 = (1/(1-psi*gamma))*(1-psi)*( ( eta1*(1+iss) + eta2*((1+iss)^2) + eta3*((1+iss)^3) +eta4*((1+iss)^4) +eta5*((1+iss)^5) +eta6*((1+iss)^6)  +eta7*((1+iss)^7)  +eta8*((1+iss)^8)  )^(-1) )*(  1-  (1/(1-theta))*( ( (1-theta)/theta )^theta )*( rss^theta)*( wss^(1-theta) )  );  
//g2_t8 = (1-g1_t8)*(yss_hss) - (delta*kss_hss);
g2_t8 = (1-g1_t8)*(yss_hss) - (delta*kss_hss)- tau*(yss_hss);
g1 = g1_t8; 
g2 = g2_t8;
css_hss = g2_t8; 
etas = ( eta1*(1+iss) + eta2*((1+iss)^2) + eta3*((1+iss)^3) +eta4*((1+iss)^4) +eta5*((1+iss)^5) +eta6*((1+iss)^6) +eta7*((1+iss)^7) +eta8*((1+iss)^8) );
@#endif

hss= (wss*(css_hss^(-sigc))*((1-hb)^(-sigc))*(1-hb*gamma))^(1/(sigh+sigc)); 
css=(css_hss)*hss;
yss = (((theta/(1-theta))*(wss/rss))^theta)*hss;
kss = (yss_hss^(1/theta))*hss;

rdss = g1*yss;
x=((nfss)^(1+alfa))/rdss; % x is (kappa_bar*delta) in the paper

pfss = (1/(1-psi))*etas*rdss; % pfss is the steady state value of th epresent value of monopoly profits stream 
lamdass = (1-hb*gamma)*((1-hb)*css)^(-sigc); % lamdass = the steady state velu of CHI in the paper
mss = (((1-hb)*css)^(sigc/sigm))*(((1+iss)/iss)^(1/sigm))*((1-hb*gamma)^(-1/sigm));
ivss = delta*kss;
ggss =tau*yss; % ggss=staedy state value of gg, govt expnditure

@#endif
//** End of data_techzero==1 section **//


@#endif




@#if tech==0

@#if data_techzero==0  

@#if eshock==16

@#if pindex==1
 index=aa0(j,17); 
 xxx = aa0(j,18);
 xpai = aa0(j,19); 
 xy =aa0(j,20); 

hb=aa0(j,21); % Internal habit formation param.
rho =aa0(j,22);  
//sigh =aa0(j,23); 
sigc =aa0(j,23); 
sigm = 2.56; 
ss=aa0(j,24);
phi = aa0(j,25);

 rhou = aa0(j,26);
 rhoi = aa0(j,27);
 rhoud = 0.5;
 rhouh = aa0(j,28);
 rhouc = aa0(j,29); 
 rhouf = aa0(j,30); 
 @#endif 

 @#if pindex==0
xxx = aa0(j,17);
xpai = aa0(j,18); 
xy =aa0(j,19); 

hb=aa0(j,20); % Internal habit formation param.
rho =aa0(j,21);  //** rho=0 is flexible price
//sigh =aa0(j,27); 
sigc =aa0(j,22); 
sigm = 2.56; //* CEL=9
ss=aa0(j,23);
phi = aa0(j,24);

rhou = aa0(j,25);
rhoi = aa0(j,26);
rhoud = 0.5;
rhouh = aa0(j,27); 
rhouc = aa0(j,28); 
rhouf = aa0(j,29); 
@#endif

@#endif


rhoum =0.9;
//sigc=1;
gamma =0.99; % discount factor  
delta = 0.10000000/4;  %capital depreciation rate
theta = 1/3;
notec=0;
tau=0.18;
//phi=6.0;
sigh=2;

iss_noa = (1/gamma)-1;
rss_noa = iss_noa + delta;
wss_noa = (((phi-1)/phi)*(1-theta)*(((1-theta)/theta)^(-theta))*(rss_noa^(-theta)))^(1/(1-theta));
yss_hss_noa = ((theta/(1-theta))*(wss_noa/rss_noa))^theta;
kss_hss_noa = (yss_hss_noa)^(1/theta);
css_hss_noa = yss_hss_noa - delta*kss_hss_noa- tau*yss_hss_noa; //* including the gov expenditure part "-tau*yss_hss_noa"
hss_noa = (wss_noa*(css_hss_noa^(-sigc))*((1-hb)^(-sigc))*(1-hb*gamma))^(1/(sigh+sigc));
yss_noa = (((theta/(1-theta))*(wss_noa/rss_noa))^theta)*hss_noa;
kss_noa = (yss_hss_noa^(1/theta))*hss_noa;
css_noa = css_hss_noa*hss_noa;
mss_noa = (((1-hb)*css_noa)^(sigc/sigm))*(((1+iss_noa)/iss_noa)^(1/sigm))*((1-hb*gamma)^(-1/sigm));
ivss_noa = delta*kss_noa;
ggss_noa =tau*yss_noa; % ggss=staedy state value of gg, govt expnditure

lamdass_noa = (1-hb*gamma)*((1-hb)*css_noa)^(-sigc);
@#endif

@#endif




model(linear);

@#if tech==1


@#if ttb_spec==8
 rd=(1+alfa)*(eta1*nf(-7)+eta2*nf(-6)+eta3*nf(-5)+eta4*nf(-4)+eta5*nf(-3)+eta6*nf(-2)+eta7*nf(-1)+eta8*nf);
 
 @#if lag_i==0
 pf(+8)=alfa*nf+((nfss^alfa)/(epsilon*x*pfss))* (  iss*(eta1+(1+iss)*eta2+((1+iss)^2)*eta3+((1+iss)^3)*eta4+((1+iss)^4)*eta5+((1+iss)^5)*eta6 +((1+iss)^6)*eta7 +((1+iss)^7)*eta8)*(1/iss)*i(+7) +  iss*(1+iss)*(eta2+((1+iss)^1)*eta3+((1+iss)^2)*eta4+((1+iss)^3)*eta5+((1+iss)^4)*eta6 +((1+iss)^5)*eta7 +((1+iss)^6)*eta8)*(1/iss)*i(+6) + iss*((1+iss)^2)*(eta3+((1+iss)^1)*eta4+((1+iss)^2)*eta5 +((1+iss)^3)*eta6 +((1+iss)^4)*eta7 +((1+iss)^5)*eta8)*(1/iss)*i(+5) + iss*((1+iss)^3)*(eta4+((1+iss)^1)*eta5 +((1+iss)^2)*eta6 +((1+iss)^3)*eta7 +((1+iss)^4)*eta8)*(1/iss)*i(+4) + iss*((1+iss)^4)*(eta5 +((1+iss)^1)*eta6 +((1+iss)^2)*eta7 +((1+iss)^3)*eta8)*(1/iss)*i(+3) + iss*((1+iss)^5)*(eta6 +((1+iss)^1)*eta7 +((1+iss)^2)*eta8)*(1/iss)*i(+2) + iss*((1+iss)^6)*(eta7 +((1+iss)^1)*eta8)*(1/iss)*i(+1) + iss*((1+iss)^7)*(eta8)*(1/iss)*i + eta1*(1+iss)*(p(+7)-p(+8)) + eta2*((1+iss)^2)*(p(+6)-p(+8)) + eta3*((1+iss)^3)*(p(+5)-p(+8)) + eta4*((1+iss)^4)*(p(+4)-p(+8)) + eta5*((1+iss)^5)*(p(+3) -p(+8)) + eta6*((1+iss)^6)*(p(+2) -p(+8)) + eta7*((1+iss)^7)*(p(+1) -p(+8)) + eta8*((1+iss)^8)*(p -p(+8))  );    
 @#endif 
 
 @#if lag_i==1
 pf(+8)=alfa*nf+((nfss^alfa)/(epsilon*x*pfss))* (  iss*(eta1+(1+iss)*eta2+((1+iss)^2)*eta3+((1+iss)^3)*eta4+((1+iss)^4)*eta5+((1+iss)^5)*eta6 +((1+iss)^6)*eta7 +((1+iss)^7)*eta8)*(1/iss)*lagi(+7) +  iss*(1+iss)*(eta2+((1+iss)^1)*eta3+((1+iss)^2)*eta4+((1+iss)^3)*eta5+((1+iss)^4)*eta6 +((1+iss)^5)*eta7 +((1+iss)^6)*eta8)*(1/iss)*lagi(+6) + iss*((1+iss)^2)*(eta3+((1+iss)^1)*eta4+((1+iss)^2)*eta5 +((1+iss)^3)*eta6 +((1+iss)^4)*eta7 +((1+iss)^5)*eta8)*(1/iss)*lagi(+5) + iss*((1+iss)^3)*(eta4+((1+iss)^1)*eta5 +((1+iss)^2)*eta6 +((1+iss)^3)*eta7 +((1+iss)^4)*eta8)*(1/iss)*lagi(+4) + iss*((1+iss)^4)*(eta5 +((1+iss)^1)*eta6 +((1+iss)^2)*eta7 +((1+iss)^3)*eta8)*(1/iss)*lagi(+3) + iss*((1+iss)^5)*(eta6 +((1+iss)^1)*eta7 +((1+iss)^2)*eta8)*(1/iss)*lagi(+2) + iss*((1+iss)^6)*(eta7 +((1+iss)^1)*eta8)*(1/iss)*lagi(+1) + iss*((1+iss)^7)*(eta8)*(1/iss)*lagi + eta1*(1+iss)*(p(+7)-p(+8)) + eta2*((1+iss)^2)*(p(+6)-p(+8)) + eta3*((1+iss)^3)*(p(+5)-p(+8)) + eta4*((1+iss)^4)*(p(+4)-p(+8)) + eta5*((1+iss)^5)*(p(+3) -p(+8)) + eta6*((1+iss)^6)*(p(+2) -p(+8)) + eta7*((1+iss)^7)*(p(+1) -p(+8)) + eta8*((1+iss)^8)*(p -p(+8))  );    
 @#endif 

 a = (1-psi)*nf(-7) + psi*a(-1);
@#endif
 
 @#if lag_i==0
 c-hb*c(-1)= (1+gamma*hb)*(c(+1)-hb*c) - gamma*hb*(c(+2)-hb*c(+1)) - ((1-hb)/sigc)*(1-gamma*hb)*((iss/(1+iss))*(1/iss)*i + p - p(+1) + uc(+1) + ud(+1)) + ((1-hb)/sigc)*(uc + ud + gamma*hb*(uc(+2) +ud(+2))) ; 
 @#endif

 @#if lag_i==1
 c-hb*c(-1)= (1+gamma*hb)*(c(+1)-hb*c) - gamma*hb*(c(+2)-hb*c(+1)) - ((1-hb)/sigc)*(1-gamma*hb)*((iss/(1+iss))*(1/iss)*lagi + p - p(+1) + uc(+1) + ud(+1)) + ((1-hb)/sigc)*(uc + ud + gamma*hb*(uc(+2) +ud(+2))) ; 
 @#endif

sigh*h = w - uh + (1/(1-hb*gamma))*( (-sigc/(1-hb))*(c - hb*c(-1)) + uc ) + (hb*gamma/(1-hb*gamma))*( (sigc/(1-hb))*(c(+1)-hb*c) - uc(+1) - ud(+1) +ud );


@#if lag_i==0
sigm*(m-p) = (-1/(1+iss))*(1/iss)*i + um + (1/(1-hb*gamma))*( (sigc/(1-hb))*(c-hb*c(-1)) -uc + (hb*gamma)*( (-sigc/(1-hb))*(c(+1)-hb*c) + uc(+1) +ud(+1) - ud) );
@#endif

@#if lag_i==1
sigm*(m-p) = (-1/(1+iss))*(1/iss)*lagi + um + (1/(1-hb*gamma))*( (sigc/(1-hb))*(c-hb*c(-1)) -uc + (hb*gamma)*( (-sigc/(1-hb))*(c(+1)-hb*c) + uc(+1) +ud(+1) - ud) );
@#endif

lamda = (1-delta)*gamma*lamda(+1) - ((1-(1-delta)*gamma)/(1-hb*gamma))*( (sigc/(1-hb))*(c(+1)-hb*c) - (1/rss)*r(+1) - uc(+1) - ud(+1) - hb*gamma*( (sigc/(1-hb))*(c(+2)-hb*c(+1)) - (1/rss)*r(+1) -uc(+2) -ud(+2) ) );
iv =  (gamma/(1+gamma))*iv(+1) + (1/(1+gamma))*iv(-1) + (1/(ss*(1+gamma)))*lamda  + (1/(ss*(1+gamma)*(1-hb*gamma)))*( (sigc/(1-hb))*(c-hb*c(-1)) - uc - ud +gamma*hb*( (-sigc/(1-hb))*(c(+1)-hb*c) + uc(+1) +ud(+1) ) ) ;
//yss*y = css*c + ivss*iv + rdss*rd;
yss*y = css*c + ivss*iv + rdss*rd +ggss*gg;

(1-delta)*k(-1) +delta*iv - k = 0;
tiv= (ivss*iv + rdss*rd)/(ivss+rdss); //*total investment: R&D investment+ capital investment
y = (1/(phi-1))*a(-1) + u + theta*k(-1) + (1-theta)*h;
h - k(-1) - (1/rss)*r + w = 0;
pai = gamma*(pai(+1)) + (((1-gamma*rho*psi)*(1-rho*psi))/(rho*psi))*(-u + theta*(1/rss)*r +(1-theta)*w - (1/(phi-1))*a(-1)) + (gamma/(phi-1))*(a - a(-1)) - (1/(phi-1))*(a(-1) - a(-2));

@#if lag_i==0
pfss*pf - (gamma*psi)*pfss*pf(+1) + (gamma*psi)*pfss*(p - p(+1) + iss*(1/iss)*i) = (1/phi)*yss*y +((phi-1)/phi)*yss*u - ((phi-1)/phi)*(1-theta)*yss*w -  ((phi-1)/phi)*theta*yss*(1/rss)*r;
@#endif

@#if lag_i==1
pfss*pf - (gamma*psi)*pfss*pf(+1) + (gamma*psi)*pfss*(p - p(+1) + iss*(1/iss)*lagi) = (1/phi)*yss*y +((phi-1)/phi)*yss*u - ((phi-1)/phi)*(1-theta)*yss*w -  ((phi-1)/phi)*theta*yss*(1/rss)*r;
@#endif

pai = p - p(-1);
gg=(1/tau)*uf +y; // * govt expenditure
//tfp=(1)*((1/(phi-1))*a(-1) + u) ;

y_g=y-y(-1);
c_g=c-c(-1);
tiv_g=tiv-tiv(-1);
iv_g=iv-iv(-1);
h_g=h-h(-1);
w_g=w-w(-1);
tfp_g=tfp-tfp(-1);

// tfp with measurement errors//
@#if tfpshock==0
tfpe= 0.9999*tfpe(-1)+tfp_g+shk_tfp*etfp; 
@#endif
@#if tfpshock==1
tfpe= 0.9999*tfpe(-1)+tfp_g+etfp; 
@#endif

@#if lag_i==0
rr=i-pai(+1); //*real interest rate
@#endif

@#if lag_i==1
rr=lagi-pai(+1); //*real interest rate
@#endif

pf_exp=pf(+8);
///////////////////////////////////////////////////////////////
// Monetary Policy (Inerest rate rule) & interest rate shock //
//////////////////////////////////////////////////////////////
iss*(1/iss)*i = ((1+iss)^(xxx))*(xxx*(iss/(1+iss))*(1/iss)*i(-1) + (1-xxx)*((xpai)*(pai)+(xy)*(yg)) + v);

@#if lag_i==1
lagi=i(-1);
@#endif

@#if ishock==1
 v = rhoi*v(-1) + ei;
@#endif

@#if ishock==0
 v = rhoi*v(-1)+ shk_i*ei;
@#endif

@#if fshock==1
uf = rhouf*uf(-1)+ef;
@#endif

@#if fshock==0
uf = rhouf*uf(-1)+shk_f*ef;
@#endif

@#if ushock==1
 @#if news==1
   

 @#if eshock==16
    u = rhou*u(-1) +eu + eue1(-1) + eue2(-2) + eue3(-3) +eue4(-4)+eue5(-5)+eue6(-6)+eue7(-7)+eue8(-8)+eue9(-9) +eue10(-10)+eue11(-11)+eue12(-12)+eue13(-13)+eue14(-14)+eue15(-15)+eue(-16);
  
 @#if var_data==22 
    conf12= eue(-12); conf11= eue11(-11);conf10= eue10(-10);conf9= eue9(-9);conf8= eue8(-8);
    @#if conf_var==1
    conf=1*((rhou^1*(u)+eue1)
       + rhou*(rhou^1*(u)+eue1)+eue2  
      + rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3
      +  rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4
      +  rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9 
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10 
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13)+eue14
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13)+eue14)+eue15
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13)+eue14)+eue15)+eue 
      )/1;
   @#endif
   @#if conf_var==2
    //conf=rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13)+eue14)+eue15)+eue ;
   //conf=rhou*(rhou^1*(u)+eue1)+eue2; 
     conf=rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4;
   @#endif

    //conf=rhou*(rhou^1*(u)+eue1)+eue2;
   @#endif 

  @#endif  
  
   
 @#endif

 @#if news==0

 
 @#if eshock==16
    u = rhou*u(-1) +eu + shk_eue*eue1(-1) + shk_eue*eue2(-2) + shk_eue*eue3(-3) +shk_eue*eue4(-4)+shk_eue*eue5(-5)+shk_eue*eue6(-6)+shk_eue*eue7(-7)+shk_eue*eue8(-8)+shk_eue*eue9(-9) +shk_eue*eue10(-10)+shk_eue*eue11(-11)+shk_eue*eue12(-12)+shk_eue*eue13(-13)+shk_eue*eue14(-14)+shk_eue*eue15(-15)+shk_eue*eue(-16); 
     @#if var_data==20
    conf12= 0; conf11=0;conf10= 0;conf9=0;conf8= 0;
    @#if conf_var==1
    conf=0;
   @#endif
   @#if conf_var==2
      conf=0;
   @#endif
    @#endif

   
    @#if var_data==22 
    conf12= 0; conf11=0;conf10= 0;conf9=0;conf8= 0;
    @#if conf_var==1
    conf=0;
   @#endif
   @#if conf_var==2
    conf=0;
   @#endif

  @#if var_data==25
    conf12= 0; conf11=0;conf10= 0;conf9=0;conf8= 0;
   @#if conf_var==1
     conf=0;
     @#endif
     @#if conf_var==2
     conf=0;
     @#endif
    @#endif

 @#endif 


   @#endif  
  //@#endif




 @#endif
@#endif 

@#if ushock==0
 @#if news==1
   
    
   @#if eshock==16
    u = rhou*u(-1) + shk_u*eu+ eue1(-1) + eue2(-2) + eue3(-3) +eue4(-4)+eue5(-5)+eue6(-6)+eue7(-7)+eue8(-8)+eue9(-9) +eue10(-10)+eue11(-11)+eue12(-12)+eue13(-13)+eue14(-14)+eue15(-15)+eue(-16); 
     @#if var_data==20
    conf12= eue(-12); conf11= eue11(-11);conf10= eue10(-10);conf9= eue9(-9);conf8= eue8(-8);
     //conf=rhou*(rhou^1*(u)+eue1)+eue2; 
   @#if conf_var==1
    conf=1*((rhou^1*(u)+eue1)
       + rhou*(rhou^1*(u)+eue1)+eue2  
      + rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3
      +  rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4
      +  rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9 
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10 
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)*eue10)+eue11)+eue12
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13)+eue14
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13)+eue14)+eue15
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13)+eue14)+eue15)+eue 
      )/16;
   @#endif
   @#if conf_var==2
    //conf=rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13)+eue14)+eue15)+eue ;
      conf=1*(( eue1)
       + rhou*(eue1)+eue2  
      + rhou*(rhou*( eue1)+eue2)+eue3
      +  rhou*(rhou*(rhou*( eue1)+eue2)+eue3)+eue4
      +  rhou*(rhou*(rhou*(rhou*( eue1)+eue2)+eue3)+eue4)+eue5
      +  rhou*(rhou*(rhou*(rhou*(rhou*( eue1)+eue2)+eue3)+eue4)+eue5)+eue6
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*( eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*( eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*( eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9 
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*( eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10 
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*( eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)*eue10)+eue11
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*( eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*( eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*( eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13)+eue14
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*( eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13)+eue14)+eue15
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13)+eue14)+eue15)+eue 
      )/1;
   @#endif
    @#endif

   @#if var_data==22 
    conf12= eue(-12); conf11= eue11(-11);conf10= eue10(-10);conf9= eue9(-9);conf8= eue8(-8);
    @#if conf_var==1
    conf=1*((rhou^1*(u)+eue1)
       + rhou*(rhou^1*(u)+eue1)+eue2  
      + rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3
      +  rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4
      +  rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9 
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10 
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13)+eue14
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13)+eue14)+eue15
      +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13)+eue14)+eue15)+eue 
      )/1;
   @#endif
   @#if conf_var==2
    //conf=rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13)+eue14)+eue15)+eue ;
   //conf=rhou*(rhou^1*(u)+eue1)+eue2; 
     conf=rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4;
   @#endif

 @#endif 


   @#endif  
  @#endif


 @#if news==0



  @#if eshock==16
    u = rhou*u(-1)+ + shk_u*eu+shk_eue*eue1(-1) + shk_eue*eue2(-2) + shk_eue*eue3(-3) +shk_eue*eue4(-4)+shk_eue*eue5(-5)+shk_eue*eue6(-6)+shk_eue*eue7(-7)+shk_eue*eue8(-8)+shk_eue*eue9(-9) +shk_eue*eue10(-10)+shk_eue*eue11(-11)+shk_eue*eue12(-12)+shk_eue*eue13(-13)+shk_eue*eue14(-14)+shk_eue*eue15(-15)+shk_eue*eue(-16); 
     @#if var_data==20
    conf12= 0; conf11=0;conf10= 0;conf9=0;conf8= 0;
    @#if conf_var==1
    conf=0;
   @#endif
   @#if conf_var==2
      conf=0;
   @#endif
    @#endif

   @#if var_data==22 
    conf12= 0; conf11=0;conf10= 0;conf9=0;conf8= 0;
    @#if conf_var==1
    conf=0;
   @#endif
   @#if conf_var==2
    conf=0;
   @#endif

   @#endif 
  @#endif  
  //@#endif





 @#endif 
@#endif



 
////////////////////////
// Preference shocks //
//////////////////////
@#if cshock==0
uc = rhouc*uc(-1)+shk_c*euc;
@#endif
@#if cshock==1
uc = rhouc*uc(-1) +euc; 
@#endif

@#if hshock==0
uh = rhouh*uh(-1)+shk_h*euh; //* Preference shock
@#endif
@#if hshock==1
uh = rhouh*uh(-1) +euh; //* Preference shock
@#endif

@#if dshock==0
ud = rhoud*ud(-1);
@#endif
@#if dshock==1
ud = rhoud*ud(-1)+eud;
@#endif



um = rhoum*um(-1);

yg=y-ynobs; //* gdp gap with measurement errors
hg = h-hn;

pinf= (gamma/(phi-1))*(a - a(-1)) - (1/(phi-1))*(a(-1) - a(-2));
mcgap=(-u + theta*(1/rss)*r +(1-theta)*w - (1/(phi-1))*an(-1)); //* (mc - mcn)
mcgape=mcgap+mee; 
mc=-u + theta*(1/rss)*r +(1-theta)*w; //*real marginal cost under sticky price
//mc=-(1/(phi-1))*a(-1)-u + theta*(1/rss)*r +(1-theta)*w; //*this real marginal cost  under sticky price
mce=mc+mee;
fpai = pai(+1);
ppai = pai(-1);
fpaie = pai(+1)+meee;
paiee=pai+meee+me;//*pai with 2sls errors & measument errors
paie=pai+me;//*pai with measument errors
ppaie=paie(-1);//*ppai with measument errors


inftr =  (((1-gamma*rho*psi)*(1-rho*psi))/(rho*psi))*(- (1/(phi-1))*a(-1)) + (gamma/(phi-1))*(a - a(-1)) - (1/(phi-1))*(a(-1) - a(-2)); //*the residural part of inflation eqn
tfp=(1)*((1/(phi-1))*a(-1) + u) ;
//tfp= u ;

tfpu=u;
shu=eu;
shi=ei;
shc=euc;
shh=euh;
shue1=eue1;
shue3=eue3;
shue4=eue4;
shue8=eue8;
aa=a(-1);

tq=lamda-(1/(1-gamma*hb))*( (sigc/(1-hb))*(-c+hb*c(-1))+uc+ud - gamma*hb*( (sigc/(1-hb))*(-c(+1)+hb*c) + uc(+1)+ud(+1) ) );
sp=tq+k(-1); 
stk=pf+sp;
//*stk is the sum of market value of capital and the intermediate firm *//  
//* q = stock price/capita *//  


//////////////////////////
//* natural variables *//
////////////////////////
 
@#if ttb_spec==8
 rdn=(1+alfa)*(eta1*nfn(-7)+eta2*nfn(-6)+eta3*nfn(-5)+eta4*nfn(-4)+eta5*nfn(-3)+eta6*nfn(-2)+eta7*nfn(-1)+eta8*nfn);
 
 @#if lag_i==0
 pfn(+8)=alfa*nfn+((nfss^alfa)/(epsilon*x*pfss))* (  iss*(eta1+(1+iss)*eta2+((1+iss)^2)*eta3+((1+iss)^3)*eta4+((1+iss)^4)*eta5+((1+iss)^5)*eta6 +((1+iss)^6)*eta7 +((1+iss)^7)*eta8)*(1/iss)*in(+7) +  iss*(1+iss)*(eta2+((1+iss)^1)*eta3+((1+iss)^2)*eta4+((1+iss)^3)*eta5+((1+iss)^4)*eta6 +((1+iss)^5)*eta7 +((1+iss)^6)*eta8)*(1/iss)*in(+6) + iss*((1+iss)^2)*(eta3+((1+iss)^1)*eta4+((1+iss)^2)*eta5 +((1+iss)^3)*eta6 +((1+iss)^4)*eta7 +((1+iss)^5)*eta8)*(1/iss)*in(+5) + iss*((1+iss)^3)*(eta4+((1+iss)^1)*eta5 +((1+iss)^2)*eta6 +((1+iss)^3)*eta7 +((1+iss)^4)*eta8)*(1/iss)*in(+4) + iss*((1+iss)^4)*(eta5 +((1+iss)^1)*eta6 +((1+iss)^2)*eta7 +((1+iss)^3)*eta8)*(1/iss)*in(+3) + iss*((1+iss)^5)*(eta6 +((1+iss)^1)*eta7 +((1+iss)^2)*eta8)*(1/iss)*in(+2) + iss*((1+iss)^6)*(eta7 +((1+iss)^1)*eta8)*(1/iss)*in(+1) + iss*((1+iss)^7)*(eta8)*(1/iss)*in + eta1*(1+iss)*(pn(+7)-pn(+8)) + eta2*((1+iss)^2)*(pn(+6)-pn(+8)) + eta3*((1+iss)^3)*(pn(+5)-pn(+8)) + eta4*((1+iss)^4)*(pn(+4)-pn(+8)) + eta5*((1+iss)^5)*(pn(+3) -pn(+8)) + eta6*((1+iss)^6)*(pn(+2) -pn(+8)) + eta7*((1+iss)^7)*(pn(+1) -pn(+8)) + eta8*((1+iss)^8)*(pn -pn(+8))  );    
 @#endif
 
 @#if lag_i==1
 pfn(+8)=alfa*nfn+((nfss^alfa)/(epsilon*x*pfss))* (  iss*(eta1+(1+iss)*eta2+((1+iss)^2)*eta3+((1+iss)^3)*eta4+((1+iss)^4)*eta5+((1+iss)^5)*eta6 +((1+iss)^6)*eta7 +((1+iss)^7)*eta8)*(1/iss)*lagin(+7) +  iss*(1+iss)*(eta2+((1+iss)^1)*eta3+((1+iss)^2)*eta4+((1+iss)^3)*eta5+((1+iss)^4)*eta6 +((1+iss)^5)*eta7 +((1+iss)^6)*eta8)*(1/iss)*lagin(+6) + iss*((1+iss)^2)*(eta3+((1+iss)^1)*eta4+((1+iss)^2)*eta5 +((1+iss)^3)*eta6 +((1+iss)^4)*eta7 +((1+iss)^5)*eta8)*(1/iss)*lagin(+5) + iss*((1+iss)^3)*(eta4+((1+iss)^1)*eta5 +((1+iss)^2)*eta6 +((1+iss)^3)*eta7 +((1+iss)^4)*eta8)*(1/iss)*lagin(+4) + iss*((1+iss)^4)*(eta5 +((1+iss)^1)*eta6 +((1+iss)^2)*eta7 +((1+iss)^3)*eta8)*(1/iss)*lagin(+3) + iss*((1+iss)^5)*(eta6 +((1+iss)^1)*eta7 +((1+iss)^2)*eta8)*(1/iss)*lagin(+2) + iss*((1+iss)^6)*(eta7 +((1+iss)^1)*eta8)*(1/iss)*lagin(+1) + iss*((1+iss)^7)*(eta8)*(1/iss)*lagin + eta1*(1+iss)*(pn(+7)-pn(+8)) + eta2*((1+iss)^2)*(pn(+6)-pn(+8)) + eta3*((1+iss)^3)*(pn(+5)-pn(+8)) + eta4*((1+iss)^4)*(pn(+4)-pn(+8)) + eta5*((1+iss)^5)*(pn(+3) -pn(+8)) + eta6*((1+iss)^6)*(pn(+2) -pn(+8)) + eta7*((1+iss)^7)*(pn(+1) -pn(+8)) + eta8*((1+iss)^8)*(pn -pn(+8))  );    
 @#endif

 an = (1-psi)*nfn(-7) + psi*an(-1);
@#endif
 
@#if lag_i==0
cn-hb*cn(-1)= (1+gamma*hb)*(cn(+1)-hb*cn) - gamma*hb*(cn(+2)-hb*cn(+1)) - ((1-hb)/sigc)*(1-gamma*hb)*((iss/(1+iss))*(1/iss)*in + pn - pn(+1) + uc(+1) + ud(+1)) + ((1-hb)/sigc)*(uc + ud + gamma*hb*(uc(+2) +ud(+2))) ; 
@#endif

@#if lag_i==1
cn-hb*cn(-1)= (1+gamma*hb)*(cn(+1)-hb*cn) - gamma*hb*(cn(+2)-hb*cn(+1)) - ((1-hb)/sigc)*(1-gamma*hb)*((iss/(1+iss))*(1/iss)*lagin + pn - pn(+1) + uc(+1) + ud(+1)) + ((1-hb)/sigc)*(uc + ud + gamma*hb*(uc(+2) +ud(+2))) ; 
@#endif


sigh*hn = wn - uh + (1/(1-hb*gamma))*( (-sigc/(1-hb))*(cn - hb*cn(-1)) + uc ) + (hb*gamma/(1-hb*gamma))*( (sigc/(1-hb))*(cn(+1)-hb*cn) - uc(+1) - ud(+1) +ud );

@#if lag_i==0
sigm*(0-pn) = (-1/(1+iss))*(1/iss)*in + um + (1/(1-hb*gamma))*( (sigc/(1-hb))*(cn-hb*cn(-1)) -uc + (hb*gamma)*( (-sigc/(1-hb))*(cn(+1)-hb*cn) + uc(+1) +ud(+1) - ud) );
@#endif
@#if lag_i==1
sigm*(0-pn) = (-1/(1+iss))*(1/iss)*lagin + um + (1/(1-hb*gamma))*( (sigc/(1-hb))*(cn-hb*cn(-1)) -uc + (hb*gamma)*( (-sigc/(1-hb))*(cn(+1)-hb*cn) + uc(+1) +ud(+1) - ud) );
@#endif



lamdan = (1-delta)*gamma*lamdan(+1) - ((1-(1-delta)*gamma)/(1-hb*gamma))*( (sigc/(1-hb))*(cn(+1)-hb*cn) - (1/rss)*rn(+1) - uc(+1) - ud(+1) - hb*gamma*( (sigc/(1-hb))*(cn(+2)-hb*cn(+1)) - (1/rss)*rn(+1) -uc(+2) -ud(+2) ) );
ivn =  (gamma/(1+gamma))*ivn(+1) + (1/(1+gamma))*ivn(-1) + (1/(ss*(1+gamma)))*lamdan  + (1/(ss*(1+gamma)*(1-hb*gamma)))*( (sigc/(1-hb))*(cn-hb*cn(-1)) - uc - ud +gamma*hb*( (-sigc/(1-hb))*(cn(+1)-hb*cn) + uc(+1) +ud(+1) ) ) ;
//yss*yn = css*cn + ivss*ivn + rdss*rdn;
yss*yn = css*cn + ivss*ivn + rdss*rdn +ggss*ggn;

(1-delta)*kn(-1) +delta*ivn - kn = 0;
tivn= (ivss*ivn + rdss*rdn)/(ivss+rdss);
ynobs=yn;
hn - kn(-1) - (1/rss)*rn + wn = 0;
yn = (1/(phi-1))*an(-1) + u + theta*kn(-1) + (1-theta)*hn;
0 = (((1-gamma*0*psi)*(1-0*psi)))*(-u + theta*(1/rss)*rn +(1-theta)*wn - (1/(phi-1))*an(-1)) ;

@#if lag_i==0
pfss*pfn - (gamma*psi)*pfss*pfn(+1) + (gamma*psi)*pfss*(pn - pn(+1) + iss*(1/iss)*in) = (1/phi)*yss*yn +((phi-1)/phi)*yss*u - ((phi-1)/phi)*(1-theta)*yss*wn -  ((phi-1)/phi)*theta*yss*(1/rss)*rn;
@#endif

@#if lag_i==1
pfss*pfn - (gamma*psi)*pfss*pfn(+1) + (gamma*psi)*pfss*(pn - pn(+1) + iss*(1/iss)*lagin) = (1/phi)*yss*yn +((phi-1)/phi)*yss*u - ((phi-1)/phi)*(1-theta)*yss*wn -  ((phi-1)/phi)*theta*yss*(1/rss)*rn;
@#endif

pain = pn - pn(-1);

tfpn=((1/(phi-1))*an(-1) + u);
aan=an(-1);
tqn=lamdan-(1/(1-gamma*hb))*( (sigc/(1-hb))*(-cn+hb*cn(-1))+uc+ud - gamma*hb*( (sigc/(1-hb))*(-cn(+1)+hb*cn) + uc(+1)+ud(+1) ) );
//tqn=-(lamdan-(1/(1-gamma*hb))*( (sigc/(1-hb))*(-cn+hb*cn(-1))+uc+ud - gamma*hb*( (sigc/(1-hb))*(-cn(+1)+hb*cn) + uc(+1)+ud(+1) ) ));

mcn=-u + theta*(1/rss)*rn +(1-theta)*wn;
ggn= (1/tau)*uf +yn;

end;

initval;
ynobs=0;
tiv=0;
p = 0; 
pai = 0;
u = 0;
ud= 0;
uc= 0;
uh= 0;
um= 0;
gg=0;
uf=0;

a = 0;
i=0;
lamda =0;
iv=0;

v=0;


nf=0;
nfn=0;


y =0 ;
k = 0;
h = 0;
iv=0;
c = 0;
m = 0;
w =0 ;
r = 0;
rd = 0;
pf =0;
//q=0;
//eue=0;

yn =0 ;
kn = 0;
hn = 0;
cn = 0;
ggn=0;
//mn = 0;
wn =0 ;
rn = 0;
rdn = 0;
pfn =0;
pn = 0; 
pain = 0;
an = 0;
//in=0;
ivn=0; 
lamdan=0;
//qn=0;


hg=0;
yg=0;

end;

steady_state_model;
ynobs=0;
tiv=0;
p = 0; 
pai = 0;
u = 0;
ud= 0;
uc= 0;
uh= 0;
um= 0;
uf=0;

a = 0;
i=0;
lamda =0;


v=0;


nf=0;
nfn=0;


y =0 ;
k = 0;
h = 0;
iv=0;
c = 0;
m = 0;
w =0 ;
r = 0;
rd = 0;
gg=0;
pf =0;
//q=0;
//eue=0;

yn =0 ;
kn = 0;
hn = 0;
cn = 0;
//mn = 0;
wn =0 ;
rn = 0;
rdn = 0;
ggn=0;
pfn =0;
pn = 0; 
pain = 0;
an = 0;
in=0;
ivn=0; 
lamdan=0;
//qn=0;


hg=0;
yg=0;

end;

@#endif


@#if tech==0

@#if lag_i==0
c-hb*c(-1)= (1+gamma*hb)*(c(+1)-hb*c) - gamma*hb*(c(+2)-hb*c(+1)) - ((1-hb)/sigc)*(1-gamma*hb)*((iss_noa/(1+iss_noa))*(1/iss_noa)*i + p - p(+1) + uc(+1) + ud(+1)) + ((1-hb)/sigc)*(uc + ud + gamma*hb*(uc(+2) +ud(+2))) ; 
@#endif
@#if lag_i==1
c-hb*c(-1)= (1+gamma*hb)*(c(+1)-hb*c) - gamma*hb*(c(+2)-hb*c(+1)) - ((1-hb)/sigc)*(1-gamma*hb)*((iss_noa/(1+iss_noa))*(1/iss_noa)*lagi + p - p(+1) + uc(+1) + ud(+1)) + ((1-hb)/sigc)*(uc + ud + gamma*hb*(uc(+2) +ud(+2))) ; 
@#endif

sigh*h = w - uh + (1/(1-hb*gamma))*( (-sigc/(1-hb))*(c - hb*c(-1)) + uc ) + (hb*gamma/(1-hb*gamma))*( (sigc/(1-hb))*(c(+1)-hb*c) - uc(+1) - ud(+1) +ud );

@#if lag_i==0
sigm*(m-p) = (-1/(1+iss_noa))*(1/iss_noa)*i + um + (1/(1-hb*gamma))*( (sigc/(1-hb))*(c-hb*c(-1)) -uc + (hb*gamma)*( (-sigc/(1-hb))*(c(+1)-hb*c) + uc(+1) +ud(+1) - ud) );
@#endif
@#if lag_i==1
sigm*(m-p) = (-1/(1+iss_noa))*(1/iss_noa)*lagi + um + (1/(1-hb*gamma))*( (sigc/(1-hb))*(c-hb*c(-1)) -uc + (hb*gamma)*( (-sigc/(1-hb))*(c(+1)-hb*c) + uc(+1) +ud(+1) - ud) );
@#endif


lamda = (1-delta)*gamma*lamda(+1) - ((1-(1-delta)*gamma)/(1-hb*gamma))*( (sigc/(1-hb))*(c(+1)-hb*c) - (1/rss_noa)*r(+1) - uc(+1) - ud(+1) - hb*gamma*( (sigc/(1-hb))*(c(+2)-hb*c(+1)) - (1/rss_noa)*r(+1) -uc(+2) -ud(+2) ) );
iv =  (gamma/(1+gamma))*iv(+1) + (1/(1+gamma))*iv(-1) + (1/(ss*(1+gamma)))*lamda  + (1/(ss*(1+gamma)*(1-hb*gamma)))*( (sigc/(1-hb))*(c-hb*c(-1)) - uc - ud +gamma*hb*( (-sigc/(1-hb))*(c(+1)-hb*c) + uc(+1) +ud(+1) ) ) ;


yss_noa*y = css_noa*c + ivss_noa*iv +ggss_noa*gg;
(1-delta)*k(-1) +delta*iv - k = 0;

y = u + theta*k(-1) + (1-theta)*h;
h - k(-1) - (1/rss_noa)*r + w = 0;

//pai = gamma*(pai(+1)) + (((1-gamma*rho)*(1-rho))/(rho))*(-u + theta*(1/rss_noa)*r +(1-theta)*w ) ;
@#if pindex==0
pai = gamma*(pai(+1)) + (((1-gamma*rho)*(1-rho))/(rho))*(-u + theta*(1/rss_noa)*r +(1-theta)*w ) ;
@#endif
@#if pindex==1
pai = ( gamma/(1+gamma*rho*index) )*(pai(+1)) + ( index/(1+gamma*rho*index) )*pai(-1)+ (  ((1-gamma*rho)*(1-rho))/( (rho)*(1+gamma*rho*index) )  )*(-u + theta*(1/rss_noa)*r +(1-theta)*w ); 
@#endif

pai = p - p(-1);


gg=(1/tau)*uf +y; // * govt expenditure

y_g=y-y(-1);
c_g=c-c(-1);
iv_g=iv-iv(-1);
h_g=h-h(-1);
w_g=w-w(-1);
tfp_g=tfp-tfp(-1);

// tfp with measurement errors//
@#if tfpshock==0
tfpe= 0.9999*tfpe(-1)+tfp_g+shk_tfp*etfp; 
@#endif
@#if tfpshock==1
tfpe= 0.9999*tfpe(-1)+tfp_g+etfp; 
@#endif


tq=lamda-(1/(1-gamma*hb))*( (sigc/(1-hb))*(-c+hb*c(-1))+uc+ud - gamma*hb*( (sigc/(1-hb))*(-c(+1)+hb*c) + uc(+1)+ud(+1) ) );
sp=(tq+k(-1));
stk=sp;

@#if lag_i==0
rr=i-pai(+1); //*real interest rate
@#endif
@#if lag_i==1
rr=lagi-pai(+1); //*real interest rate
@#endif


///////////////////////////////////////////////////////////////
// Monetary Policy (Inerest rate rule) & interest rate shock //
//////////////////////////////////////////////////////////////
iss_noa*(1/iss_noa)*i = ((1+iss_noa)^(xxx))*(xxx*(iss_noa/(1+iss_noa))*(1/iss_noa)*i(-1) + (1-xxx)*((xpai)*(pai)+(xy)*(yg)) + v);

@#if lag_i==1
lagi=i(-1);
@#endif

@#if ishock==1
v = rhoi*v(-1) + ei;
@#endif

@#if ishock==0
 v = rhoi*v(-1)+ shk_i*ei;
@#endif

@#if fshock==1
uf = rhouf*uf(-1)+ef;
@#endif

@#if fshock==0
uf = rhouf*uf(-1)+shk_f*ef;
@#endif

@#if ushock==1
 @#if news==1
   
  
   @#if eshock==16
   u = rhou*u(-1) +eu + eue1(-1) + eue2(-2) + eue3(-3) + eue4(-4)+ eue5(-5)+ eue6(-6)+ eue7(-7)+ eue8(-8)+ eue9(-9) +eue10(-10)+eue11(-11)+eue12(-12)+eue13(-13)+eue14(-14)+eue15(-15)+eue(-16); 
   
   @#if var_data==22  
    conf12= eue(-12); conf11= eue11(-11);conf10= eue10(-10);conf9= eue9(-9);conf8= eue8(-8);
    //conf=rhou*(rhou^1*(u)+eue1)+eue2;
    conf=((rhou^1*(u)+eue1)
        + rhou*(rhou^1*(u)+eue1)+eue2  
        + rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3
        +  rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4
        +  rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5
        +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6
        +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7
        +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8
        +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9 
        +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10 
        +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11
        +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12
        +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13
        +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13)+eue14
        +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13)+eue14)+eue15
        +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13)+eue14)+eue15)+eue 
        )/1;
   @#endif

   
   @#endif
   
 @#endif
 @#if news==0
   

 @#if eshock==16
    u = rhou*u(-1)+ eu + shk_eue*eue1(-1) + shk_eue*eue2(-2) + shk_eue*eue3(-3) +shk_eue*eue4(-4)+shk_eue*eue5(-5)+shk_eue*eue6(-6)+shk_eue*eue7(-7)+shk_eue*eue8(-8)+shk_eue*eue9(-9) +shk_eue*eue10(-10)+shk_eue*eue11(-11)+shk_eue*eue12(-12)+shk_eue*eue13(-13)+shk_eue*eue14(-14)+shk_eue*eue15(-15)+shk_eue*eue(-16); 
     @#if var_data==20
    conf12= 0; conf11=0;conf10= 0;conf9=0;conf8= 0;
    @#if conf_var==1
    conf=0;
   @#endif
   @#if conf_var==2
      conf=0;
   @#endif
    @#endif

   @#if var_data==22 
    conf12= 0; conf11=0;conf10= 0;conf9=0;conf8= 0;
    @#if conf_var==1
    conf=0;
   @#endif
   @#if conf_var==2
    conf=0;
   @#endif

  @#endif 
   @#endif  
 



 @#endif
@#endif 

@#if ushock==0
 @#if news==1
   
   @#if eshock==16
   u = rhou*u(-1) + shk_u*eu+ eue1(-1) + eue2(-2) + eue3(-3) + eue4(-4)+ eue5(-5)+ eue6(-6)+ eue7(-7)+ eue8(-8)+ eue9(-9) +eue10(-10)+eue11(-11)+eue12(-12)+eue13(-13)+eue14(-14)+eue15(-15)+eue(-16); 
   
  @#if var_data==22  
    conf12= eue(-12); conf11= eue11(-11);conf10= eue10(-10);conf9= eue9(-9);conf8= eue8(-8);
    //conf=rhou*(rhou^1*(u)+eue1)+eue2;
   conf=((rhou^1*(u)+eue1)
        + rhou*(rhou^1*(u)+eue1)+eue2  
        + rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3
        +  rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4
        +  rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5
        +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6
        +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7
        +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8
        +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9 
        +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10 
        +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11
        +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12
        +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13
        +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13)+eue14
        +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13)+eue14)+eue15
        +  rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou*(rhou^1*(u)+eue1)+eue2)+eue3)+eue4)+eue5)+eue6)+eue7)+eue8)+eue9)+eue10)+eue11)+eue12)+eue13)+eue14)+eue15)+eue 
        )/1;
   @#endif
  
  @#endif

 @#endif


 @#if news==0



 @#if eshock==16
    u = rhou*u(-1)+ shk_u*eu + shk_eue*eue1(-1) + shk_eue*eue2(-2) + shk_eue*eue3(-3) +shk_eue*eue4(-4)+shk_eue*eue5(-5)+shk_eue*eue6(-6)+shk_eue*eue7(-7)+shk_eue*eue8(-8)+shk_eue*eue9(-9) +shk_eue*eue10(-10)+shk_eue*eue11(-11)+shk_eue*eue12(-12)+shk_eue*eue13(-13)+shk_eue*eue14(-14)+shk_eue*eue15(-15)+shk_eue*eue(-16); 
     @#if var_data==20
    conf12= 0; conf11=0;conf10= 0;conf9=0;conf8= 0;
    @#if conf_var==1
    conf=0;
   @#endif
   @#if conf_var==2
      conf=0;
   @#endif
    @#endif

   @#if var_data==22 
    conf12= 0; conf11=0;conf10= 0;conf9=0;conf8= 0;
    @#if conf_var==1
    conf=0;
   @#endif
   @#if conf_var==2
    conf=0;
   @#endif

  @#endif 
   @#endif  
  


 @#endif 
@#endif

 
////////////////////////
// Preference shocks //
//////////////////////
@#if dshock==1
ud = rhoud*ud(-1) +eud; //* Preference shock
@#endif

@#if dshock==0
ud = rhoud*ud(-1); //* Preference shock
@#endif

@#if cshock==1
uc = rhouc*uc(-1) +euc; //* Preference shock
@#endif

@#if cshock==0
uc = rhouc*uc(-1)+shk_c*euc; //* Preference shock
@#endif


@#if hshock==1
uh = rhouh*uh(-1) +euh; //* Preference shock
@#endif

@#if hshock==0
uh = rhouh*uh(-1)+shk_h*euh; //* Preference shock
@#endif


//um = rhoum*um(-1) +eum; //* Preference shock
um = rhoum*um(-1) ; //* Preference shock


//yg=y-yn; //* gdp gap
yg=y-ynobs; //* gdp gap with measurement errors
hg = h-hn;


mcgap=(-u + theta*(1/rss_noa)*r +(1-theta)*w );
mcgape=mcgap+mee;
mc=mcgap;
mce=mcgape;
//gap_nw=(-u + theta*(1/rss_noa)*r +(1-theta)*w );
fpai = pai(+1);
ppai = pai(-1);
fpaie = pai(+1)+meee;
paiee=pai+meee+me;//*pai with 2sls errors & measument errors
paie=pai+me;//*pai with measument errors
ppaie=paie(-1);

tfp=u;
tfpu=u;
shu=eu;
shi=ei;
shc=euc;
shh=euh;
shue1=eue1;

/////////////////////////
//* natural variables *//
////////////////////////

@#if lag_i==0
cn-hb*cn(-1)= (1+gamma*hb)*(cn(+1)-hb*cn) - gamma*hb*(cn(+2)-hb*cn(+1)) - ((1-hb)/sigc)*(1-gamma*hb)*((iss_noa/(1+iss_noa))*(1/iss_noa)*in + pn - pn(+1) + uc(+1) + ud(+1)) + ((1-hb)/sigc)*(uc + ud + gamma*hb*(uc(+2) +ud(+2))) ; 
@#endif
@#if lag_i==1
cn-hb*cn(-1)= (1+gamma*hb)*(cn(+1)-hb*cn) - gamma*hb*(cn(+2)-hb*cn(+1)) - ((1-hb)/sigc)*(1-gamma*hb)*((iss_noa/(1+iss_noa))*(1/iss_noa)*lagin + pn - pn(+1) + uc(+1) + ud(+1)) + ((1-hb)/sigc)*(uc + ud + gamma*hb*(uc(+2) +ud(+2))) ; 
@#endif

sigh*hn = wn - uh + (1/(1-hb*gamma))*( (-sigc/(1-hb))*(cn - hb*cn(-1)) + uc ) + (hb*gamma/(1-hb*gamma))*( (sigc/(1-hb))*(cn(+1)-hb*cn) - uc(+1) - ud(+1) +ud );

@#if lag_i==0
sigm*(0-pn) = (-1/(1+iss_noa))*(1/iss_noa)*in + um + (1/(1-hb*gamma))*( (sigc/(1-hb))*(cn-hb*cn(-1)) -uc + (hb*gamma)*( (-sigc/(1-hb))*(cn(+1)-hb*cn) + uc(+1) +ud(+1) - ud) );
@#endif
@#if lag_i==1
sigm*(0-pn) = (-1/(1+iss_noa))*(1/iss_noa)*lagin + um + (1/(1-hb*gamma))*( (sigc/(1-hb))*(cn-hb*cn(-1)) -uc + (hb*gamma)*( (-sigc/(1-hb))*(cn(+1)-hb*cn) + uc(+1) +ud(+1) - ud) );
@#endif

lamdan = (1-delta)*gamma*lamdan(+1) - ((1-(1-delta)*gamma)/(1-hb*gamma))*( (sigc/(1-hb))*(cn(+1)-hb*cn) - (1/rss_noa)*rn(+1) - uc(+1) - ud(+1) - hb*gamma*( (sigc/(1-hb))*(cn(+2)-hb*cn(+1)) - (1/rss_noa)*rn(+1) -uc(+2) -ud(+2) ) );
ivn =  (gamma/(1+gamma))*ivn(+1) + (1/(1+gamma))*ivn(-1) + (1/(ss*(1+gamma)))*lamdan  + (1/(ss*(1+gamma)*(1-hb*gamma)))*( (sigc/(1-hb))*(cn-hb*cn(-1)) - uc - ud +gamma*hb*( (-sigc/(1-hb))*(cn(+1)-hb*cn) + uc(+1) +ud(+1) ) ) ;

 yss_noa*yn = css_noa*cn + ivss_noa*ivn +ggss_noa*ggn;
(1-delta)*kn(-1) +delta*ivn - kn = 0;
//ynobs=yn+me;
ynobs=yn;

hn - kn(-1) - (1/rss_noa)*rn + wn = 0;
yn = u + theta*kn(-1) + (1-theta)*hn;
0 = (((1-gamma*0)*(1-0)))*(-u + theta*(1/rss_noa)*rn +(1-theta)*wn ) ;

pain = pn - pn(-1);
//mn = mn(-1);
//mn = 0;

//tqn = sigc*cn+lamdan; //*Tobin's Q
tfpn=u;
tqn=lamdan-(1/(1-gamma*hb))*( (sigc/(1-hb))*(-cn+hb*cn(-1))+uc+ud - gamma*hb*( (sigc/(1-hb))*(-cn(+1)+hb*cn) + uc(+1)+ud(+1) ) );
//tqn=-(lamdan-(1/(1-gamma*hb))*( (sigc/(1-hb))*(-cn+hb*cn(-1))+uc+ud - gamma*hb*( (sigc/(1-hb))*(-cn(+1)+hb*cn) + uc(+1)+ud(+1) ) ));


mcn=(-u + theta*(1/rss_noa)*rn +(1-theta)*wn );

ggn= (1/tau)*uf +yn;

end;


initval;

//sh=0;
p = 0; 
pai = 0;
u = 0;
ud= 0;
uc= 0;
uh= 0;
um= 0;


i=0;
lamda =0;
iv=0;


v=0;



y =0 ;
k = 0;
h = 0;
iv=0;
c = 0;
m = 0;
w =0 ;
r = 0;

//q=0;
//eue=0;

yn =0 ;
kn = 0;
hn = 0;
cn = 0;
//mn = 0;
wn =0 ;
rn = 0;
//rdn = 0;

pn = 0; 
pain = 0;

//in=0;
ivn=0; 
lamdan=0;
//qn=0;


hg=0;
yg=0;

end;

steady_state_model;
p = 0; 
pai = 0;
u = 0;
ud= 0;
uc= 0;
uh= 0;
um= 0;


i=0;
lamda =0;
//tiv=0;

v=0;




y =0 ;
k = 0;
h = 0;
iv=0;
c = 0;
m = 0;
w =0 ;
r = 0;

//q=0;
//eue=0;

yn =0 ;
kn = 0;
hn = 0;
cn = 0;
//mn = 0;
wn =0 ;
rn = 0;

//pfn =0;
pn = 0; 
pain = 0;

in=0;
ivn=0; 
lamdan=0;
//qn=0;


hg=0;
yg=0;
end;
@#endif


 shocks;


 var me;  // measurement errors  for 'mc'
 stderr me_std; 

 var mee;  // measurement errors  for 'mc'
 stderr mee_std;

 var meee;  // measurement errors for 'fpai'
 stderr meee_std;


//***********************************//
//**** Start of fig==0 Section **////
//***********************************//
@#if fig==0
@#if data_techzero==0  
 var eu;
 stderr aa0(j,1); 
 
 var ei;
 stderr aa0(j,2);

 var euc;
 stderr aa0(j,3);

  var euh;
 stderr aa0(j,4);

  var ef;
 stderr aa0(j,5);

 // measurement errors  for 'TFP growth'
var etfp;
 stderr tfpwt*aa0(j,10);

@#endif

@#if data_techzero==1  
 var eu;
stderr aa0(j,1); 
//stderr 0; 
 
 var ei;
@#if var_shock==0
stderr aa0(j,2);
@#endif
@#if var_shock==1
stderr 0; 
@#endif
 
var euc;
@#if var_shock==0
stderr aa0(j,3);
@#endif
@#if var_shock==1
stderr 0; 
@#endif 

var euh;
@#if var_shock==0
stderr aa0(j,4);
@#endif
@#if var_shock==1
stderr 0; 
@#endif 


var ef;
@#if var_shock==0
stderr aa0(j,5);
@#endif
@#if var_shock==1
stderr 0; 
@#endif 

// measurement errors  for 'TFP growth'
var etfp; 
 stderr tfpwt*aa0(j,10);


@#endif
   

///////////////////
// NEWS SHOCKS//
//////////////////
@#if data_techzero==0 


  var eue1;
 stderr 0;

  var eue2;
 stderr 0;

  var eue3;
 stderr 0;

  var eue4;
 stderr aa0(j,6);

  var eue5;
 stderr 0;

  var eue6;
 stderr 0;

  var eue7;
 stderr 0;

 var eue8;
 stderr aa0(j,7);

  var eue9;
 stderr 0;


@#if eshock==16 
  var eue10;
 stderr 0;
  
 var eue11;
 stderr 0;

  var eue12;
 stderr aa0(j,8);

 var eue13;
 stderr 0;
 
 var eue14;
 stderr 0;

 var eue15;
 stderr 0;
 
 var eue;
 stderr aa0(j,9);


@#if crosscrr==1
corr eue4, eue8 =aa0(j,11); corr eue4, eue12 =aa0(j,12); corr eue4, eue =aa0(j,13); corr eue4, eu =0;
corr eue8, eue12= aa0(j,14); corr eue8, eue =aa0(j,15); corr eue8, eu =0;
corr eue12, eue =aa0(j,16); corr eue12, eu =0;
corr eue, eu =0;
@#endif



@#endif

   
@#endif

@#if data_techzero==1 

  var eue1;
 stderr 0;

  var eue2;
 stderr 0;

  var eue3;
 stderr 0;

  var eue4;
 stderr aa0(j,6);

  var eue5;
 stderr 0;

  var eue6;
 stderr 0;

  var eue7;
 stderr 0;

 var eue8;
 stderr aa0(j,7);

  var eue9;
 stderr 0;

  var eue10;
  stderr 0;



@#if eshock==16
 var eue11;
 stderr 0;

  var eue12;
 stderr aa0(j,8);

 var eue13;
 stderr 0;
 
 var eue14;
 stderr 0;

 var eue15;
 stderr 0;
 
 var eue;
 stderr aa0(j,9);


@#if crosscrr==1
corr eue4, eue8 =aa0(j,11); corr eue4, eue12 =aa0(j,12); corr eue4, eue =aa0(j,13); corr eue4, eu =0;
corr eue8, eue12 = aa0(j,14); corr eue8, eue =aa0(j,15); corr eue8, eu =0;
corr eue12, eue =aa0(j,16); corr eue12, eu =0;
corr eue, eu =0;
@#endif

@#endif

 

@#endif

  
@#endif

//@#endif
//***********************************//
//** End of fig==0 section **//
//***********************************//


//***********************************//
//** Start fig==1 section **//
//***********************************//
@#if fig==1

@#if cndvar==0

@#if std_spec==0

@#if data_techzero==0  
 @#if shku==1
 var eu;
 stderr 0.01; 
 @#endif
 @#if shku==0
 var eu;
 stderr 0; 
 @#endif

 @#if shki==1
 var ei;
 stderr 0.01;
 @#endif
 @#if shki==0
 var ei;
 stderr 0;
 @#endif

 @#if shkuc==1
 var euc;
 stderr 0.01;
 @#endif
 @#if shkuc==0
 var euc;
 stderr 0;
 @#endif

 @#if shkuh==1
 var euh;
 stderr 0.01;
 @#endif
 @#if shkuh==0
 var euh;
 stderr 0;
 @#endif 

 @#if shkf==1
 var ef;
 stderr 0.01;
 @#endif
 @#if shkf==0
 var ef;
 stderr 0;
 @#endif 


@#endif

@#if data_techzero==1  
 @#if shku==1
 var eu;
 stderr 0.01; 
 @#endif
 @#if shku==0
 var eu;
 stderr 0; 
 @#endif

 @#if shki==1
 var ei;
 stderr 0.01;
 @#endif
 @#if shki==0
 var ei;
 stderr 0;
 @#endif

 @#if shkuc==1
 var euc;
 stderr 0.01;
 @#endif
 @#if shkuc==0
 var euc;
 stderr 0;
 @#endif

 @#if shkuh==1
 var euh;
 stderr 0.01;
 @#endif
 @#if shkuh==0
 var euh;
 stderr 0;
 @#endif 

 @#if shkf==1
 var ef;
 stderr 0.01;
 @#endif
 @#if shkf==0
 var ef;
 stderr 0;
 @#endif 

@#endif



///////////////////
// NEWS SHOCKS//
//////////////////
 @#if shkue1==1
  var eue1;
 stderr 0.01;
 @#endif
  @#if shkue1==0
  var eue1=0;
 @#endif
 
 @#if shkue2==1
  var eue2;
 stderr 0.01;
 @#endif
 @#if shkue2==0
  var eue2=0;
 @#endif
 
 @#if shkue3==1
  var eue3;
 stderr 0.01;
 @#endif
 @#if shkue3==0
  var eue3=0;
 @#endif

 @#if shkue4==1
  var eue4;
 stderr 0.01;
 @#endif
  @#if shkue4==0
  var eue4=0;
 @#endif

@#if shkue5==1
  var eue5;
 stderr 0.01;
 @#endif
 
@#if shkue5==0
  var eue5=0;
 @#endif

@#if shkue6==1
  var eue6;
 stderr 0.01;
 @#endif
 
 @#if shkue6==0
  var eue6=0;
 @#endif

@#if shkue7==1
  var eue7;
 stderr 0.01;
 @#endif
 
 @#if shkue7==0
  var eue7=0;
 @#endif

@#if shkue8==1
  var eue8;
 stderr 0.01;
 @#endif
 
 @#if shkue8==0
  var eue8=0;
 @#endif

@#if shkue9==1
  var eue9;
 stderr 0.01;
 @#endif
 
 @#if shkue9==0
  var eue9=0;
 @#endif

@#if eshock==10
 @#if shkue==1
  var eue;
  stderr 0.01;
  @#endif
 
  @#if shkue==0
  var eue=0;
  @#endif
@#endif


 
@#if eshock==16
  @#if shkue10==1
  var eue10;
  stderr 0.01;
  @#endif
 
  @#if shkue10==0
  var eue10=0;
  @#endif
  
  @#if shkue11==1
  var eue11;
  stderr 0.01;
  @#endif
 
  @#if shkue11==0
  var eue11=0;
  @#endif
   
  @#if shkue12==1
  var eue12;
  stderr 0.01;
  @#endif
 
  @#if shkue12==0
  var eue12=0;
  @#endif

  @#if shkue13==1
  var eue13;
  stderr 0.01;
  @#endif
 
  @#if shkue13==0
  var eue13=0;
  @#endif
  

  @#if shkue14==1
  var eue14;
  stderr 0.01;
  @#endif
 
  @#if shkue14==0
  var eue14=0;
  @#endif

  @#if shkue15==1
  var eue15;
  stderr 0.01;
  @#endif
 
  @#if shkue15==0
  var eue15=0;
  @#endif

   @#if shkue==1
  var eue;
  stderr 0.01;
  @#endif
 
  @#if shkue==0
  var eue=0;
  @#endif

@#endif

  
 //@#endif

@#endif



@#if std_spec==1

@#if data_techzero==0  
 @#if shku==1
 var eu;
 stderr aa0(j,1); 
 @#endif
 @#if shku==0
 var eu;
 stderr 0; 
 @#endif

 @#if shki==1
 var ei;
 stderr aa0(j,2);
 @#endif
 @#if shki==0
 var ei;
 stderr 0;
 @#endif

 @#if shkuc==1
 var euc;
 stderr aa0(j,3);
 @#endif
 @#if shkuc==0
 var euc;
 stderr 0;
 @#endif

 @#if shkuh==1
 var euh;
 stderr aa0(j,4);
 @#endif
 @#if shkuh==0
 var euh;
 stderr 0;
 @#endif 

  @#if shkf==1
 var ef;
 stderr aa0(j,5);
 @#endif
 @#if shkf==0
 var ef;
 stderr 0;
 @#endif 

@#endif

@#if data_techzero==1  
 @#if shku==1
 var eu;
 stderr aa0(j,1); 
 @#endif
 @#if shku==0
 var eu;
 stderr 0; 
 @#endif

 @#if shki==1
 var ei;
 stderr aa0(j,2);
 @#endif
 @#if shki==0
 var ei;
 stderr 0;
 @#endif

 @#if shkuc==1
 var euc;
 stderr aa0(j,3);
 @#endif
 @#if shkuc==0
 var euc;
 stderr 0;
 @#endif

 @#if shkuh==1
 var euh;
 stderr aa0(j,4);
 @#endif
 @#if shkuh==0
 var euh;
 stderr 0;
 @#endif
 
 @#if shkf==1
 var ef;
 stderr aa0(j,5);
 @#endif
 @#if shkf==0
 var ef;
 stderr 0;
 @#endif
 
@#endif



///////////////////
// NEWS SHOCKS//
//////////////////
 @#if shkue1==1
  var eue1;
 stderr aa0(j,6);
 @#endif
  @#if shkue1==0
  var eue1=0;
 @#endif
 
 @#if shkue2==1
  var eue2;
 stderr aa0(j,7);
 @#endif
 @#if shkue2==0
  var eue2=0;
 @#endif
 
 @#if shkue3==1
  var eue3;
 stderr aa0(j,8);
 @#endif
 @#if shkue3==0
  var eue3=0;
 @#endif

 @#if shkue4==1
  var eue4;
 stderr aa0(j,9);
 @#endif
  @#if shkue4==0
  var eue4=0;
 @#endif

@#if shkue5==1
  var eue5;
 stderr aa0(j,10);
 @#endif
 
@#if shkue5==0
  var eue5=0;
 @#endif

@#if shkue6==1
  var eue6;
 stderr aa0(j,11);
 @#endif
 
 @#if shkue6==0
  var eue6=0;
 @#endif

@#if shkue7==1
  var eue7;
 stderr aa0(j,12);
 @#endif
 
 @#if shkue7==0
  var eue7=0;
 @#endif


@#if shkue8==1
  var eue8;
 stderr aa0(j,13);
 @#endif
 
 @#if shkue8==0
  var eue8=0;
 @#endif


@#if shkue9==1
  var eue9;
 stderr aa0(j,14);
 @#endif
 
 @#if shkue9==0
  var eue9=0;
 @#endif

@#if eshock==10
  @#if shkue==1
  var eue;
  stderr aa0(j,15);
  @#endif
 
  @#if shkue==0
  var eue=0;
  @#endif
@#endif

@#if eshock==12
  @#if shkue10==1
  var eue10;
  stderr aa0(j,14);
  @#endif
 
  @#if shkue10==0
  var eue10=0;
  @#endif
  
  @#if shkue11==1
  var eue11;
  stderr aa0(j,15);
  @#endif
 
  @#if shkue11==0
  var eue11=0;
  @#endif

  @#if shkue==1
  var eue;
  stderr aa0(j,16);
  @#endif
 
  @#if shkue==0
  var eue=0;
  @#endif

`@#endif



@#if eshock==14
  @#if shkue10==1
  var eue10;
  stderr aa0(j,14);
  @#endif
 
  @#if shkue10==0
  var eue10=0;
  @#endif
  
  @#if shkue11==1
  var eue11;
  stderr aa0(j,15);
  @#endif
 
  @#if shkue11==0
  var eue11=0;
  @#endif

   @#if shkue12==1
  var eue12;
  stderr aa0(j,16);
  @#endif
 
  @#if shkue12==0
  var eue12=0;
  @#endif

  @#if shkue13==1
  var eue13;
  stderr aa0(j,17);
  @#endif
 
  @#if shkue13==0
  var eue13=0;
  @#endif 
  
  @#if shkue==1
  var eue;
  stderr aa0(j,18);
  @#endif
 
  @#if shkue==0
  var eue=0;
  @#endif

@#endif






@#if eshock==16
  @#if shkue10==1
  var eue10;
  stderr aa0(j,15);
  @#endif
 
  @#if shkue10==0
  var eue10=0;
  @#endif
  
  @#if shkue11==1
  var eue11;
  stderr aa0(j,16);
  @#endif
 
  @#if shkue11==0
  var eue11=0;
  @#endif

   @#if shkue12==1
  var eue12;
  stderr aa0(j,17);
  @#endif
 
  @#if shkue12==0
  var eue12=0;
  @#endif

  @#if shkue13==1
  var eue13;
  stderr aa0(j,18);
  @#endif
 
  @#if shkue13==0
  var eue13=0;
  @#endif 
  
  @#if shkue14==1
  var eue14;
  stderr aa0(j,19);
  @#endif
 
  @#if shkue14==0
  var eue14=0;
  @#endif

  @#if shkue15==1
  var eue15;
  stderr aa0(j,20);
  @#endif
 
  @#if shkue15==0
  var eue15=0;
  @#endif 
 
  @#if shkue==1
  var eue;
  stderr aa0(j,21);
  @#endif
 
  @#if shkue==0
  var eue=0;
  @#endif


@#if crosscrr==1
corr eue4, eue8 =aa0(j,38); corr eue4, eue12 =aa0(j,39); corr eue4, eue =aa0(j,40); corr eue4, eu =aa0(j,41);
corr eue8, eue12 = aa0(j,42); corr eue8, eue =aa0(j,43); corr eue8, eu =aa0(j,44);
corr eue12, eue =aa0(j,45); corr eue12, eu =aa0(j,46);
corr eue, eu =aa0(j,47);
@#endif

`@#endif


@#endif

@#endif




@#if cndvar==1

@#if data_techzero==0  
 var eu;
 stderr aa0(j,1); 
 
 var ei;
 stderr aa0(j,2);

 var euc;
 stderr aa0(j,3);

  var euh;
 stderr aa0(j,4);
@#endif

@#if data_techzero==1  
 var eu;
 stderr aa0(j,1); 
 
 var ei;
 stderr aa0(j,2);

 var euc;
 stderr aa0(j,3);

  var euh;
 stderr aa0(j,4);

 var ef;
 stderr aa0(j,5);
@#endif
   

///////////////////
// NEWS SHOCKS//
//////////////////
@#if data_techzero==0 

  var eue1;
 stderr aa0(j,6);

  var eue2;
 stderr aa0(j,7);

  var eue3;
 stderr aa0(j,8);

  var eue4;
 stderr aa0(j,9);

  var eue5;
 stderr aa0(j,10);

  var eue6;
 stderr aa0(j,11);

  var eue7;
 stderr aa0(j,12);

 var eue8;
 stderr aa0(j,13);

  var eue9;
 stderr aa0(j,14);
 



@#if eshock==16
 var eue10;
 stderr aa0(j,15);
  
 var eue11;
 stderr aa0(j,16);

  var eue12;
 stderr aa0(j,17);

 var eue13;
 stderr aa0(j,18);

  var eue14;
 stderr aa0(j,19);

 var eue15;
 stderr aa0(j,20);
 
 var eue;
 stderr aa0(j,21);


@#if crosscrr==1
corr eue4, eue8 =aa0(j,35); corr eue4, eue12 =aa0(j,36); corr eue4, eue =aa0(j,37); corr eue4, eu =aa0(j,38);
corr eue8, eue12= aa0(j,39); corr eue8, eue =aa0(j,40); corr eue8, eu =aa0(j,41);
corr eue12, eue =aa0(j,42); corr eue12, eu =aa0(j,43);
corr eue, eu =aa0(j,44);
@#endif


@#endif


@#endif

@#if data_techzero==1 

  var eue1;
 stderr aa0(j,6);

  var eue2;
 stderr aa0(j,7);

  var eue3;
 stderr aa0(j,8);

  var eue4;
 stderr aa0(j,9);

  var eue5;
 stderr aa0(j,10);

  var eue6;
 stderr aa0(j,11);

  var eue7;
 stderr aa0(j,12);

 var eue8;
 stderr aa0(j,13);

  var eue9;
 stderr aa0(j,14);
 

@#if eshock==16
 var eue10;
 stderr aa0(j,15);
  
 var eue11;
 stderr aa0(j,16);

  var eue12;
 stderr aa0(j,17);

 var eue13;
 stderr aa0(j,18);

  var eue14;
 stderr aa0(j,19);

 var eue15;
 stderr aa0(j,20);
 
 var eue;
 stderr aa0(j,21);

@#if crosscrr==1
corr eue4, eue8 =aa0(j,35); corr eue4, eue12 =aa0(j,36); corr eue4, eue =aa0(j,37); corr eue4, eu =aa0(j,38);
corr eue8, eue12= aa0(j,39); corr eue8, eue =aa0(j,40); corr eue8, eu =aa0(j,41);
corr eue12, eue =aa0(j,42); corr eue12, eu =aa0(j,43);
corr eue, eu =aa0(j,44);
@#endif 

@#endif


@#endif

@#endif


@#endif
@#endif
@#endif

//***********************************//
//** End of fig==1 section **//
//***********************************//
 

 
end;



for ii=iia:iib
set_dynare_seed(ii); 


@#if large_sample==0

@#if tech==1
 @#if fig==0
  @#if hp==0
    @#if var_data==22
    stoch_simul(irf=40, order=2,periods=700,nograph, noprint ,qz_criterium=0.999999, drop=0)  tfpe y a u c iv tiv k h rd pai fpai p tq mc nf  pf_exp w r rr i tfp tfpu  yn an cn ivn tivn kn hn rdn pain tqn mcn nfn sp conf pf stk y_g  c_g  tiv_g iv_g  h_g  w_g tfp_g;    
     @#endif
   @#endif
 @#endif
 
 @#if fig==1
 @#if cndvar==0
  @#if full_fig==0 
  stoch_simul(irf=40, order=1,periods=580,nograph, drop=10) y a u c iv tiv k h rd pai fpai p  tq mc nf  pf pf_exp w r rr i tfp tfpu  yn an cn ivn tivn kn hn rdn pain tqn mcn nfn sp  conf pf stk y_g  c_g  tiv_g iv_g  h_g  w_g tfp_g;  
  @#endif
  @#if full_fig==1 
  stoch_simul(irf=40, order=1,periods=580, drop=10) y a u c iv tiv k h rd pai fpai p  tq mc nf  pf pf_exp w r rr i tfp tfpu  yn an cn ivn tivn kn hn rdn pain tqn mcn nfn sp  conf pf stk y_g  c_g  tiv_g iv_g  h_g  w_g tfp_g;  
  @#endif 
 @#endif 
 @#if cndvar==1
 stoch_simul(irf=40, order=1,periods=0,nograph, drop=10,conditional_variance_decomposition = [ 8 12 16 20 40]) y a u c iv tiv k h rd pai fpai p  tq mc nf   pf pf_exp w r rr i tfp tfpu  yn an cn ivn tivn kn hn rdn pain tqn mcn nfn sp  conf pf stk y_g  c_g  tiv_g iv_g  h_g  w_g tfp_g;  
 //**The above is for varaince decomposition: the conditional variance requires theoretical moment, that is periods==0******//
 @#endif
@#endif
@#endif

@#if tech==0
 @#if fig==0
   @#if hp==0
    @#if var_data==22
    stoch_simul(irf=40, order=1,periods=700,nograph, noprint ,qz_criterium=0.999999, drop=10)  tfpe y yn u mc c iv  k  h  pai fpai p  w r rr i tfp tfpu  tq tqn cn ivn kn hn pain mcn sp  conf   stk y_g  c_g  h_g iv_g  w_g tfp_g; 
    @#endif
 @#endif
  @#if hp==1
   stoch_simul(irf=40, order=1,periods=580,nograph,hp_filter=1600, drop=10)   y yn u mc c iv  k  h  pai fpai  p w r  rr i tfp tfpu  tq tqn yn cn ivn kn hn pain tqn mcn sp  conf   stk; 
  @#endif
 @#endif   
 @#if fig==1
 @#if cndvar==0
  @#if full_fig==0 
  stoch_simul(irf=40, order=1,periods=580,nograph, drop=10)  y yn u mc c iv  k  h  pai fpai p  w r rr i tfp tfpu  tq tqn yn cn ivn kn hn pain tqn mcn sp  conf  stk; 
  @#endif
  @#if full_fig==1  
  stoch_simul(irf=40, order=1,periods=580, drop=10)  y yn u mc c iv  k  h  pai fpai p  w r rr i tfp tfpu  tq tqn yn cn ivn kn hn pain tqn mcn sp  conf stk; 
  @#endif
 @#endif
 @#if cndvar==1
 stoch_simul(irf=40, order=1,periods=0,nograph, drop=10,conditional_variance_decomposition = [1 4 8 12 16 20 40])  y yn u mc c iv  k  h  pai fpai p  w r rr i tfp tfpu  tq tqn yn cn ivn kn hn pain tqn mcn  sp  conf  stk; 
 @#endif
 @#endif   
@#endif

@#endif



//*******************************************************//
//*** Creating simulated dataset for monte carolo VAR ***// 
//*******************************************************//

@#if sim_var==1

/* labor prodcuctivity */
//yl=y-h;


 
/* disricarding first 400 obs*/
@#if tech==1

@#if bps==0

@#if trend_sigma==0
intvar_monte_data1{:,1}=y(inds:inde);
intvar_monte_data1{:,2}=c(inds:inde);
 @#if var_price==0
 intvar_monte_data1{:,3}=pai(inds:inde);
 @#endif
 @#if var_price==1
 intvar_monte_data1{:,3}=p(inds:inde);
 @#endif
%intvar_monte_data1{:,4}=w(inds:inde);
intvar_monte_data1{:,4}=h(inds:inde);
%intvar_monte_data1{:,5}=yl(inds:inde);

@#if var_variable==1
intvar_monte_data1{:,5}=i(inds:inde);
@#endif
@#if var_variable==2
  @#if tfp_errors==1
  intvar_monte_data1{:,5}=tfpe(inds:inde);
  @#endif
  @#if tfp_errors==0
  intvar_monte_data1{:,5}=tfp(inds:inde);
  @#endif
intvar_monte_data1{:,6}=i(inds:inde);
@#endif


@#endif


@#endif


@#endif

@#if tech==0

@#if bps==0

@#if trend_sigma==0
intvar_monte_data1{:,1}=y(inds:inde);
intvar_monte_data1{:,2}=c(inds:inde);
 @#if var_price==0
 intvar_monte_data1{:,3}=pai(inds:inde);
 @#endif
 @#if var_price==1
 intvar_monte_data1{:,3}=p(inds:inde);
 @#endif
%intvar_monte_data1{:,4}=w(inds:inde);
intvar_monte_data1{:,4}=h(inds:inde);
%intvar_monte_data1{:,5}=yl(inds:inde);

%intvar_monte_data1{:,6}=a(inds:inde);
%intvar_monte_data1{:,7}=i(inds:inde);

@#if var_variable==1
intvar_monte_data1{:,5}=i(inds:inde);
@#endif
@#if var_variable==2
 @#if tfp_errors==1
 intvar_monte_data1{:,5}=tfpe(inds:inde);
 @#endif
 @#if tfp_errors==0
 intvar_monte_data1{:,5}=tfp(inds:inde);
 @#endif
intvar_monte_data1{:,6}=i(inds:inde);
@#endif

@#endif


@#endif

@#endif

intvar_monte_data=cell2mat(intvar_monte_data1);
intvar_monte_data_i{:,:,ii}=intvar_monte_data;
intvar_monte_data_ii=cell2mat(intvar_monte_data_i);
save 'intvar_monte_data' intvar_monte_data;
%save 'var_monte_data_ii'

GO_VAR3_cp1;
%KurmannOtrok_main_sw_var3;


@#if var_est_method==2

@#if var_data==22

 @#if median_mean==1
 intvar_monte_y_med(:,ii,j)=IRFMED(1:62,1,6);
 intvar_monte_c_med(:,ii,j)=IRFMED(1:62,2,6);
 @#if var_price==0
 intvar_monte_pai_med(:,ii,j)=IRFMED(1:62,3,6);
 @#endif
 @#if var_price==1
 intvar_monte_pai_med(:,ii,j)=diff(IRFMED(1:63,3,6));
 @#endif
 %intvar_monte_w_med(:,ii,j)=IRFMED(1:62,4,6);
 intvar_monte_h_med(:,ii,j)=IRFMED(1:62,4,6);
 %intvar_monte_i_med(:,ii,j)=IRFMED(1:62,5,7);
 %intvar_monte_i_med(:,ii,j)=IRFMED(1:62,7,7);

 @#if var_variable==1
 intvar_monte_i_med(:,ii,j)=IRFMED(1:62,6,6);
 @#endif
 @#if var_variable==2
 intvar_monte_tfp_med(:,ii,j)=IRFMED(1:62,6,6);
 intvar_monte_i_med(:,ii,j)=IRFMED(1:62,7,6);
 @#endif


 @#endif 

 @#if median_mean==2
 intvar_monte_y_med(:,ii,j)=IRFBAR(1:62,1,6);
 intvar_monte_c_med(:,ii,j)=IRFBAR(1:62,2,6);
 @#if var_price==0
 intvar_monte_pai_med(:,ii,j)=IRFBAR(1:62,3,6);
 @#endif
 @#if var_price==1
 intvar_monte_pai_med(:,ii,j)=diff(IRFBAR(1:63,3,6));
 @#endif 
 intvar_monte_w_med(:,ii,j)=IRFBAR(1:62,4,6);
 intvar_monte_h_med(:,ii,j)=IRFBAR(1:62,5,6);
 %intvar_monte_i_med(:,ii,j)=IRFBAR(1:62,6,7);
 %intvar_monte_i_med(:,ii,j)=IRFBAR(1:62,7,7);

 @#if var_variable==1
 intvar_monte_i_med(:,ii,j)=IRBAR(1:62,6,6);
 @#endif
 @#if var_variable==2
 intvar_monte_tfp_med(:,ii,j)=IRFBAR(1:62,6,6);
 intvar_monte_i_med(:,ii,j)=IRFBAR(1:62,7,6);
 @#endif

 
 @#endif

@#endif 

@#endif



@#if var_est_method==0
%OLS estimation%

@#if var_data==22
 
 @#if var_variable==1
 intvar_monte_y_med(:,ii,j)=IRF(1:62,1,5);
 intvar_monte_c_med(:,ii,j)=IRF(1:62,2,5);
  @#if var_price==0
  intvar_monte_pai_med(:,ii,j)=IRF(1:62,3,5);
  @#endif
  @#if var_price==1
  diffIRF=[0;diff(IRF(1:63,3,5))];
  intvar_monte_pai_med(:,ii,j)=diffIRF(1:62);
  %intvar_monte_pai_med(:,ii,j)=diff(IRF(1:60,3,5));
  @#endif
 %intvar_monte_w_med(:,ii,j)=IRF(1:62,4,6);
 intvar_monte_h_med(:,ii,j)=IRF(1:62,4,5);
 %intvar_monte_yl_med(:,ii,j)=IRF(1:62,5,5);
 intvar_monte_i_med(:,ii,j)=IRF(1:62,5,5);
 @#endif
 
 @#if var_variable==2
  intvar_monte_y_med(:,ii,j)=IRF(1:62,1,6);
  intvar_monte_c_med(:,ii,j)=IRF(1:62,2,6);
  @#if var_price==0
  intvar_monte_pai_med(:,ii,j)=IRF(1:62,3,6);
  @#endif
  @#if var_price==1
  diffIRF=[0;diff(IRF(1:63,3,6))];
  intvar_monte_pai_med(:,ii,j)=diffIRF(1:62);
  %intvar_monte_pai_med(:,ii,j)=diff(IRF(1:63,3,6));
  @#endif
 %intvar_monte_w_med(:,ii,j)=IRF(1:62,4,6);
 intvar_monte_h_med(:,ii,j)=IRF(1:62,4,6);
%intvar_monte_yl_med(:,ii,j)=IRF(1:62,5,7);

 intvar_monte_tfp_med(:,ii,j)=IRF(1:62,5,6);
 intvar_monte_i_med(:,ii,j)=IRF(1:62,6,6);
 @#endif
  
 @#endif 




 
@#endif



@#endif
//********************************************************//


end
//** End of 'for ii' : 'ii' is for a monte carlo sim, i.e., shocks are drawn "ii" times **//



end 
//**************************************************************************************************************************************//
//** End of 'for j' : 'j' is for Bayesian coefficient estimates, i.e., different coeffifient estimates (j different coeffs) are used **//
//**************************************************************************************************************************************//



//*******************************************************************************************************//
//*** Start of Monte carlo (bayesian?) multiple news shocks IRF (can use Bayesian by choosing e.g. ja=1 &jb=200 *****//
//*******************************************************************************************************//




@#if sim_var==1

@#if var_est_method==2


@#if var_data==22


size_mat=size(intvar_monte_y_med);
rw=size_mat(1);
%cl=size_mat(2)*size_mat(3);
cl=size_mat(2);


intvar_monte_y_med_rsp_=reshape(intvar_monte_y_med,[rw cl]);
intvar_monte_c_med_rsp_=reshape(intvar_monte_c_med,[rw cl]);
intvar_monte_pai_med_rsp_=reshape(intvar_monte_pai_med,[rw cl]);
%intvar_monte_w_med_rsp_=reshape(intvar_monte_w_med,[rw cl]);
intvar_monte_h_med_rsp_=reshape(intvar_monte_h_med,[rw cl]);
%intvar_monte_a_med_rsp_=reshape(intvar_monte_a_med,[rw cl]);
intvar_monte_i_med_rsp_=reshape(intvar_monte_i_med,[rw cl]);

@#if var_variable==2
intvar_monte_tfp_med_rsp_=reshape(intvar_monte_tfp_med,[rw cl]);
@#endif

intvar_monte_y_med_rsp=intvar_monte_y_med_rsp_(:, iia:iib);
intvar_monte_c_med_rsp=intvar_monte_c_med_rsp_(:, iia:iib);
intvar_monte_pai_med_rsp=intvar_monte_pai_med_rsp_(:, iia:iib);
%intvar_monte_w_med_rsp=intvar_monte_w_med_rsp_(:, iia:iib);
intvar_monte_h_med_rsp=intvar_monte_h_med_rsp_(:, iia:iib);
%intvar_monte_a_med_rsp=intvar_monte_a_med_rsp_(:, iia:iib);
intvar_monte_i_med_rsp=intvar_monte_i_med_rsp_(:, iia:iib);

@#if var_variable==2
intvar_monte_tfp_med_rsp=intvar_monte_tfp_med_rsp_(:, iia:iib);
@#endif





@#if median_mean==1
intvar_monte_y_av_av=median(intvar_monte_y_med_rsp,2);
intvar_monte_c_av_av=median(intvar_monte_c_med_rsp,2);
intvar_monte_pai_av_av=median(intvar_monte_pai_med_rsp,2);
%intvar_monte_w_av_av=median(intvar_monte_w_med_rsp,2);
intvar_monte_h_av_av=median(intvar_monte_h_med_rsp,2);
%intvar_monte_a_av_av=median(intvar_monte_a_med_rsp,2);
intvar_monte_i_av_av=median(intvar_monte_i_med_rsp,2);
 @#if var_variable==2
 intvar_monte_tfp_av_av=median(intvar_monte_tfp_med_rsp,2);
 @#endif
@#endif

@#if median_mean==2
intvar_monte_y_av_av=mean(intvar_monte_y_med_rsp,2);
intvar_monte_c_av_av=mean(intvar_monte_c_med_rsp,2);
intvar_monte_pai_av_av=mean(intvar_monte_pai_med_rsp,2);
%intvar_monte_w_av_av=mean(intvar_monte_w_med_rsp,2);
intvar_monte_h_av_av=mean(intvar_monte_h_med_rsp,2);
%intvar_monte_a_av_av=mean(intvar_monte_a_med_rsp,2);
intvar_monte_i_av_av=mean(intvar_monte_i_med_rsp,2);
 @#if var_variable==2
 intvar_monte_tfp_av_av=mean(intvar_monte_tfp_med_rsp,2);
 @#endif
  
@#endif

intvar_monte_y_pt5=prctile(intvar_monte_y_med_rsp,5,2);
intvar_monte_y_pt95=prctile(intvar_monte_y_med_rsp,95,2);
%intvar_monte_w_pt5=prctile(intvar_monte_w_med_rsp,5,2);
%intvar_monte_w_pt95=prctile(intvar_monte_w_med_rsp,95,2);
intvar_monte_c_pt5=prctile(intvar_monte_c_med_rsp,5,2);
intvar_monte_c_pt95=prctile(intvar_monte_c_med_rsp,95,2);
intvar_monte_h_pt5=prctile(intvar_monte_h_med_rsp,5,2);
intvar_monte_h_pt95=prctile(intvar_monte_h_med_rsp,95,2);
intvar_monte_i_pt5=prctile(intvar_monte_i_med_rsp,5,2);
intvar_monte_i_pt95=prctile(intvar_monte_i_med_rsp,95,2);
intvar_monte_pai_pt5=prctile(intvar_monte_pai_med_rsp,5,2);
intvar_monte_pai_pt95=prctile(intvar_monte_pai_med_rsp,95,2);
%intvar_monte_a_pt5=prctile(intvar_monte_a_med_rsp,5,2);
%intvar_monte_a_pt95=prctile(intvar_monte_a_med_rsp,95,2);

intvar_monte_y_ptstdl=prctile(intvar_monte_y_med_rsp,16.5,2);
intvar_monte_y_ptstdh=prctile(intvar_monte_y_med_rsp,83.5,2);
%intvar_monte_w_ptstdl=prctile(intvar_monte_w_med_rsp,16.5,2);
%intvar_monte_w_ptstdh=prctile(intvar_monte_w_med_rsp,83.5,2);
intvar_monte_c_ptstdl=prctile(intvar_monte_c_med_rsp,16.5,2);
intvar_monte_c_ptstdh=prctile(intvar_monte_c_med_rsp,83.5,2);
intvar_monte_h_ptstdl=prctile(intvar_monte_h_med_rsp,16.5,2);
intvar_monte_h_ptstdh=prctile(intvar_monte_h_med_rsp,83.5,2);
intvar_monte_i_ptstdl=prctile(intvar_monte_i_med_rsp,16.5,2);
intvar_monte_i_ptstdh=prctile(intvar_monte_i_med_rsp,83.5,2);
intvar_monte_pai_ptstdl=prctile(intvar_monte_pai_med_rsp,16.5,2);
intvar_monte_pai_ptstdh=prctile(intvar_monte_pai_med_rsp,83.5,2);
%intvar_monte_a_ptstdl=prctile(intvar_monte_a_med_rsp,16.5,2);
%intvar_monte_a_ptstdh=prctile(intvar_monte_a_med_rsp,83.5,2);



@#endif




@#endif

@#if var_est_method==0



@#if var_data==22

size_mat=size(intvar_monte_y_med);
rw=size_mat(1);
%cl=size_mat(2)*size_mat(3);
cl=size_mat(2);


intvar_monte_y_med_rsp_=reshape(intvar_monte_y_med,[rw cl]);
intvar_monte_c_med_rsp_=reshape(intvar_monte_c_med,[rw cl]);
intvar_monte_pai_med_rsp_=reshape(intvar_monte_pai_med,[rw cl]);
%intvar_monte_w_med_rsp_=reshape(intvar_monte_w_med,[rw cl]);
%intvar_monte_yl_med_rsp_=reshape(intvar_monte_yl_med,[rw cl]);
intvar_monte_h_med_rsp_=reshape(intvar_monte_h_med,[rw cl]);
intvar_monte_i_med_rsp_=reshape(intvar_monte_i_med,[rw cl]);
@#if var_variable==2
intvar_monte_tfp_med_rsp_=reshape(intvar_monte_tfp_med,[rw cl]);
@#endif

intvar_monte_y_med_rsp=intvar_monte_y_med_rsp_(:, iia:iib);
intvar_monte_c_med_rsp=intvar_monte_c_med_rsp_(:, iia:iib);
intvar_monte_pai_med_rsp=intvar_monte_pai_med_rsp_(:, iia:iib);
%intvar_monte_w_med_rsp=intvar_monte_w_med_rsp_(:, iia:iib);
intvar_monte_h_med_rsp=intvar_monte_h_med_rsp_(:, iia:iib);
%intvar_monte_yl_med_rsp=intvar_monte_yl_med_rsp_(:, iia:iib);
intvar_monte_i_med_rsp=intvar_monte_i_med_rsp_(:, iia:iib);
@#if var_variable==2
intvar_monte_tfp_med_rsp=intvar_monte_tfp_med_rsp_(:, iia:iib);
@#endif




@#if median_mean==1
intvar_monte_y_av_av=median(intvar_monte_y_med_rsp,2);
intvar_monte_c_av_av=median(intvar_monte_c_med_rsp,2);
intvar_monte_pai_av_av=median(intvar_monte_pai_med_rsp,2);
%intvar_monte_w_av_av=median(intvar_monte_w_med_rsp,2);
intvar_monte_h_av_av=median(intvar_monte_h_med_rsp,2);
%intvar_monte_yl_av_av=median(intvar_monte_yl_med_rsp,2);
intvar_monte_i_av_av=median(intvar_monte_i_med_rsp,2);
@#if var_variable==2
intvar_monte_tfp_av_av=median(intvar_monte_tfp_med_rsp,2);
@#endif
@#endif

@#if median_mean==2
intvar_monte_y_av_av=mean(intvar_monte_y_med_rsp,2);
intvar_monte_c_av_av=mean(intvar_monte_c_med_rsp,2);
intvar_monte_pai_av_av=mean(intvar_monte_pai_med_rsp,2);
%intvar_monte_w_av_av=mean(intvar_monte_w_med_rsp,2);
intvar_monte_h_av_av=mean(intvar_monte_h_med_rsp,2);
%intvar_monte_yl_av_av=mean(intvar_monte_yl_med_rsp,2);
intvar_monte_i_av_av=mean(intvar_monte_i_med_rsp,2);
@#if var_variable==2
intvar_monte_tfp_av_av=mean(intvar_monte_tfp_med_rsp,2);
@#endif
@#endif

intvar_monte_y_pt5=prctile(intvar_monte_y_med_rsp,5,2);
intvar_monte_y_pt95=prctile(intvar_monte_y_med_rsp,95,2);
%intvar_monte_w_pt5=prctile(intvar_monte_w_med_rsp,5,2);
%intvar_monte_w_pt95=prctile(intvar_monte_w_med_rsp,95,2);
intvar_monte_c_pt5=prctile(intvar_monte_c_med_rsp,5,2);
intvar_monte_c_pt95=prctile(intvar_monte_c_med_rsp,95,2);
intvar_monte_h_pt5=prctile(intvar_monte_h_med_rsp,5,2);
intvar_monte_h_pt95=prctile(intvar_monte_h_med_rsp,95,2);
%intvar_monte_yl_pt5=prctile(intvar_monte_yl_med_rsp,5,2);
%intvar_monte_yl_pt95=prctile(intvar_monte_yl_med_rsp,95,2);
intvar_monte_i_pt5=prctile(intvar_monte_i_med_rsp,5,2);
intvar_monte_i_pt95=prctile(intvar_monte_i_med_rsp,95,2);
intvar_monte_pai_pt5=prctile(intvar_monte_pai_med_rsp,5,2);
intvar_monte_pai_pt95=prctile(intvar_monte_pai_med_rsp,95,2);
%intvar_monte_a_pt5=prctile(intvar_monte_a_med_rsp,5,2);
%intvar_monte_a_pt95=prctile(intvar_monte_a_med_rsp,95,2);

intvar_monte_y_ptstdl=prctile(intvar_monte_y_med_rsp,16.5,2);
intvar_monte_y_ptstdh=prctile(intvar_monte_y_med_rsp,83.5,2);
%intvar_monte_w_ptstdl=prctile(intvar_monte_w_med_rsp,16.5,2);
%intvar_monte_w_ptstdh=prctile(intvar_monte_w_med_rsp,83.5,2);
intvar_monte_c_ptstdl=prctile(intvar_monte_c_med_rsp,16.5,2);
intvar_monte_c_ptstdh=prctile(intvar_monte_c_med_rsp,83.5,2);
intvar_monte_h_ptstdl=prctile(intvar_monte_h_med_rsp,16.5,2);
intvar_monte_h_ptstdh=prctile(intvar_monte_h_med_rsp,83.5,2);
%intvar_monte_yl_ptstdl=prctile(intvar_monte_yl_med_rsp,16.5,2);
%intvar_monte_yl_ptstdh=prctile(intvar_monte_yl_med_rsp,83.5,2);
intvar_monte_i_ptstdl=prctile(intvar_monte_i_med_rsp,16.5,2);
intvar_monte_i_ptstdh=prctile(intvar_monte_i_med_rsp,83.5,2);
intvar_monte_pai_ptstdl=prctile(intvar_monte_pai_med_rsp,16.5,2);
intvar_monte_pai_ptstdh=prctile(intvar_monte_pai_med_rsp,83.5,2);
@#if var_variable==2
intvar_monte_tfp_ptstdl=prctile(intvar_monte_tfp_med_rsp,16.5,2);
intvar_monte_tfp_ptstdh=prctile(intvar_monte_tfp_med_rsp,83.5,2);
%intvar_monte_a_ptstdl=prctile(intvar_monte_a_med_rsp,16.5,2);
%intvar_monte_a_ptstdh=prctile(intvar_monte_a_med_rsp,83.5,2);
@#endif

@#endif




@#endif
@#endif

//figure('NumberTitle','off','Name','IRFs of Monte Carlo VAR:  mean')
figure(400)

@#if sim_var==1

subplot(3,3,1)
if lsp ==1
plot(intvar_monte_y_av_av,'r--');
elseif lsp ==2
plot(intvar_monte_y_av_av,'k.-');
elseif lsp ==3
plot(intvar_monte_y_av_av,'b--');
elseif lsp ==4
plot(intvar_monte_y_av_av,'k-');
end
title('y');
hold on

 
subplot(3,3,2)
if lsp ==1
plot(intvar_monte_pai_av_av,'r--');
elseif lsp ==2
plot(intvar_monte_pai_av_av,'k.-');
elseif lsp ==3
plot(intvar_monte_pai_av_av,'b--');
elseif lsp ==4
plot(intvar_monte_pai_av_av,'k-');
end
title('pai');
hold on


subplot(3,3,3)
if lsp ==1
plot(intvar_monte_c_av_av,'r--');
elseif lsp ==2
plot(intvar_monte_c_av_av,'k.-');
elseif lsp ==3
plot(intvar_monte_c_av_av,'b--');
elseif lsp ==4
plot(intvar_monte_c_av_av,'k-');
end
title('c');
hold on


subplot(3,3,4)
if lsp ==1
plot(intvar_monte_h_av_av,'r--');
elseif lsp ==2
plot(intvar_monte_h_av_av,'k.-');
elseif lsp ==3
plot(intvar_monte_h_av_av,'b--');
elseif lsp ==4
plot(intvar_monte_h_av_av,'k-');
end
%title('Labor Productivity');
title('Labor');
hold on


subplot(3,3,5)
if lsp ==1
plot(intvar_monte_i_av_av,'r--');
elseif lsp ==2
plot(intvar_monte_i_av_av,'k.-');
elseif lsp ==3
plot(intvar_monte_i_av_av,'b--');
elseif lsp ==4
plot(intvar_monte_i_av_av,'k-');
end
title('i');
hold on

%subplot(3,3,6)
%if lsp ==1
%plot(intvar_monte_w_av_av,'r--');
%elseif lsp ==2
%plot(intvar_monte_w_av_av,'k.-');
%elseif lsp ==3
%plot(intvar_monte_w_av_av,'b--');
%elseif lsp ==4
%plot(intvar_monte_w_av_av,'k-');
%end
%title('w');
%hold on

@#if var_variable==2
subplot(3,3,7)
if lsp ==1
plot(intvar_monte_tfp_av_av,'r--');
elseif lsp ==2
plot(intvar_monte_tfp_av_av,'k.-');
elseif lsp ==3
plot(intvar_monte_tfp_av_av,'b--');
elseif lsp ==4
plot(intvar_monte_tfp_av_av,'k-');
end
title('TFP');
hold on
@#endif



@#endif


figure(100)

@#if sim_var==1


@#if var_variable==1 

subplot(2,2,1)
if lsp ==1
plot(intvar_monte_pai_av_av,'r--');
elseif lsp ==2
plot(intvar_monte_pai_av_av,'k.-');
elseif lsp ==3
plot(intvar_monte_pai_av_av,'b--');
elseif lsp ==4
plot(intvar_monte_pai_av_av,'k-');
end
title('inf');
hold on

subplot(2,2,2)
if lsp ==1
plot(intvar_monte_y_av_av,'r--');
elseif lsp ==2
plot(intvar_monte_y_av_av,'k.-');
elseif lsp ==3
plot(intvar_monte_y_av_av,'b--');
elseif lsp ==4
plot(intvar_monte_y_av_av,'k-');
end
title('y');
hold on

subplot(2,2,3)
if lsp ==1
plot(intvar_monte_c_av_av,'r--');
elseif lsp ==2
plot(intvar_monte_c_av_av,'k.-');
elseif lsp ==3
plot(intvar_monte_c_av_av,'b--');
elseif lsp ==4
plot(intvar_monte_c_av_av,'k-');
end
title('c');
hold on


subplot(2,2,4)
if lsp ==1
plot(intvar_monte_i_av_av,'r--');
elseif lsp ==2
plot(intvar_monte_i_av_av,'k.-');
elseif lsp ==3
plot(intvar_monte_i_av_av,'b--');
elseif lsp ==4
plot(intvar_monte_i_av_av,'k-');
end
title('i');
hold on

@#endif

@#if var_variable==2

subplot(3,2,1)
if lsp ==1
plot(intvar_monte_pai_av_av,'r--');
elseif lsp ==2
plot(intvar_monte_pai_av_av,'k.-');
elseif lsp ==3
plot(intvar_monte_pai_av_av,'b--');
elseif lsp ==4
plot(intvar_monte_pai_av_av,'k-');
end
title('inf');
hold on

subplot(3,2,2)
if lsp ==1
plot(intvar_monte_y_av_av,'r--');
elseif lsp ==2
plot(intvar_monte_y_av_av,'k.-');
elseif lsp ==3
plot(intvar_monte_y_av_av,'b--');
elseif lsp ==4
plot(intvar_monte_y_av_av,'k-');
end
title('y');
hold on

subplot(3,2,3)
if lsp ==1
plot(intvar_monte_c_av_av,'r--');
elseif lsp ==2
plot(intvar_monte_c_av_av,'k.-');
elseif lsp ==3
plot(intvar_monte_c_av_av,'b--');
elseif lsp ==4
plot(intvar_monte_c_av_av,'k-');
end
title('c');
hold on


subplot(3,2,4)
if lsp ==1
plot(intvar_monte_i_av_av,'r--');
elseif lsp ==2
plot(intvar_monte_i_av_av,'k.-');
elseif lsp ==3
plot(intvar_monte_i_av_av,'b--');
elseif lsp ==4
plot(intvar_monte_i_av_av,'k-');
end
title('i');
hold on

subplot(3,2,5)
if lsp ==1
plot(intvar_monte_tfp_av_av,'r--');
elseif lsp ==2
plot(intvar_monte_tfp_av_av,'k.-');
elseif lsp ==3
plot(intvar_monte_tfp_av_av,'b--');
elseif lsp ==4
plot(intvar_monte_tfp_av_av,'k-');
end
title('TFP');
hold on



@#endif

@#endif



